diff -r 44b49f255e31 -r d5e6c8c92d87 rust/hwphysics/src/data.rs --- a/rust/hwphysics/src/data.rs Thu Jan 27 03:51:13 2022 +0300 +++ b/rust/hwphysics/src/data.rs Fri Jan 28 02:33:44 2022 +0300 @@ -242,7 +242,7 @@ block_masks: vec![], element_sizes: Box::new([0; 64]), element_alignments: Box::new([0; 64]), - lookup: vec![LookupEntry::default(); u16::max_value() as usize].into_boxed_slice(), + lookup: vec![LookupEntry::default(); u16::MAX as usize].into_boxed_slice(), } } @@ -486,7 +486,7 @@ pub fn register(&mut self) { debug_assert!(!std::mem::needs_drop::()); - debug_assert!(size_of::() <= u16::max_value() as usize); + debug_assert!(size_of::() <= u16::MAX as usize); let id = TypeId::of::(); if size_of::() == 0 { @@ -533,6 +533,19 @@ } } + pub fn get(&self, gear_id: GearId) -> Option<&T> { + let entry = self.lookup[gear_id.get() as usize - 1]; + match (entry.index, self.get_type_index::()) { + (Some(index), Some(type_index)) => { + let block = &self.blocks[entry.block_index as usize]; + block.component_blocks[type_index].map(|ptr| unsafe { + &*(ptr.as_ptr() as *const T).add(index.get() as usize - 1) + }) + } + _ => None, + } + } + pub fn iter(&mut self) -> DataIterator { let mut arg_types = Vec::with_capacity(64); T::get_types(&mut arg_types); @@ -614,6 +627,25 @@ struct Tag; #[test] + fn direct_access() { + let mut manager = GearDataManager::new(); + manager.register::(); + for i in 1..=5 { + manager.add(GearId::new(i as u16).unwrap(), &Datum { value: i * i }); + } + + for i in 1..=5 { + assert_eq!( + manager + .get::(GearId::new(i as u16).unwrap()) + .unwrap() + .value, + i * i + ); + } + } + + #[test] fn single_component_iteration() { let mut manager = GearDataManager::new(); manager.register::();