diff -r 27915135f87f -r 6e3e5be8b2e2 rust/hwphysics/src/common.rs --- a/rust/hwphysics/src/common.rs Wed Aug 28 22:53:40 2019 +0300 +++ b/rust/hwphysics/src/common.rs Wed Aug 28 23:06:34 2019 +0300 @@ -6,7 +6,6 @@ }; pub type GearId = NonZeroU16; -pub trait GearData {} #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)] #[repr(transparent)] @@ -37,16 +36,6 @@ } } -pub trait GearDataProcessor { - fn add(&mut self, gear_id: GearId, gear_data: T); - fn remove(&mut self, gear_id: GearId); - fn get(&mut self, gear_id: GearId) -> Option; -} - -pub trait GearDataAggregator { - fn find_processor(&mut self) -> &mut GearDataProcessor; -} - pub struct GearAllocator { max_id: u16, free_ids: BinaryHeap, @@ -73,91 +62,3 @@ self.free_ids.push(gear_id) } } - -#[derive(Clone, Copy, Default)] -pub struct LookupEntry { - index: Option, - value: T, -} - -impl LookupEntry { - #[inline] - pub fn index(&self) -> u16 { - self.index.map(|i| i.get()).unwrap_or(0) - 1 - } - - #[inline] - pub fn set_index(&mut self, index: u16) { - self.index = unsafe { Some(NonZeroU16::new_unchecked(index.saturating_add(1))) }; - } - - #[inline] - pub fn value(&self) -> &T { - &self.value - } - - #[inline] - pub fn value_mut(&mut self) -> &mut T { - &mut self.value - } - - #[inline] - pub fn set_value(&mut self, value: T) { - self.value = value; - } -} - -pub struct GearDataLookup { - lookup: Box<[LookupEntry]>, -} - -impl GearDataLookup { - pub fn new() -> Self { - Self { - lookup: vec![LookupEntry::default(); u16::max_value() as usize].into_boxed_slice(), - } - } -} - -impl GearDataLookup { - pub fn add(&mut self, gear_id: GearId, index: u16, value: T) { - // All possible Gear IDs are valid indices - let entry = unsafe { self.lookup.get_unchecked_mut(gear_id.get() as usize - 1) }; - entry.set_index(index); - entry.set_value(value); - } - - pub fn get(&self, gear_id: GearId) -> Option<&LookupEntry> { - // All possible Gear IDs are valid indices - let entry = unsafe { self.lookup.get_unchecked(gear_id.get() as usize - 1) }; - if let Some(index) = entry.index { - Some(entry) - } else { - None - } - } - - pub fn get_mut(&mut self, gear_id: GearId) -> Option<&mut LookupEntry> { - // All possible Gear IDs are valid indices - let entry = unsafe { self.lookup.get_unchecked_mut(gear_id.get() as usize - 1) }; - if let Some(index) = entry.index { - Some(entry) - } else { - None - } - } -} - -impl Index for GearDataLookup { - type Output = LookupEntry; - - fn index(&self, index: GearId) -> &Self::Output { - self.get(index).unwrap() - } -} - -impl IndexMut for GearDataLookup { - fn index_mut(&mut self, index: GearId) -> &mut Self::Output { - self.get_mut(index).unwrap() - } -}