equal
deleted
inserted
replaced
240 tags: Vec::with_capacity(64), |
240 tags: Vec::with_capacity(64), |
241 blocks: vec![], |
241 blocks: vec![], |
242 block_masks: vec![], |
242 block_masks: vec![], |
243 element_sizes: Box::new([0; 64]), |
243 element_sizes: Box::new([0; 64]), |
244 element_alignments: Box::new([0; 64]), |
244 element_alignments: Box::new([0; 64]), |
245 lookup: vec![LookupEntry::default(); u16::max_value() as usize].into_boxed_slice(), |
245 lookup: vec![LookupEntry::default(); u16::MAX as usize].into_boxed_slice(), |
246 } |
246 } |
247 } |
247 } |
248 |
248 |
249 #[inline] |
249 #[inline] |
250 fn get_type_index<T: 'static>(&self) -> Option<usize> { |
250 fn get_type_index<T: 'static>(&self) -> Option<usize> { |
484 } |
484 } |
485 } |
485 } |
486 |
486 |
487 pub fn register<T: 'static>(&mut self) { |
487 pub fn register<T: 'static>(&mut self) { |
488 debug_assert!(!std::mem::needs_drop::<T>()); |
488 debug_assert!(!std::mem::needs_drop::<T>()); |
489 debug_assert!(size_of::<T>() <= u16::max_value() as usize); |
489 debug_assert!(size_of::<T>() <= u16::MAX as usize); |
490 |
490 |
491 let id = TypeId::of::<T>(); |
491 let id = TypeId::of::<T>(); |
492 if size_of::<T>() == 0 { |
492 if size_of::<T>() == 0 { |
493 if !self.tags.contains(&id) { |
493 if !self.tags.contains(&id) { |
494 debug_assert!(self.tags.len() <= 64); |
494 debug_assert!(self.tags.len() <= 64); |
528 |
528 |
529 unsafe { |
529 unsafe { |
530 T::iter(&slices[..], block.elements_count as usize, |id, x| f(id, x)); |
530 T::iter(&slices[..], block.elements_count as usize, |id, x| f(id, x)); |
531 } |
531 } |
532 } |
532 } |
|
533 } |
|
534 } |
|
535 |
|
536 pub fn get<T: 'static>(&self, gear_id: GearId) -> Option<&T> { |
|
537 let entry = self.lookup[gear_id.get() as usize - 1]; |
|
538 match (entry.index, self.get_type_index::<T>()) { |
|
539 (Some(index), Some(type_index)) => { |
|
540 let block = &self.blocks[entry.block_index as usize]; |
|
541 block.component_blocks[type_index].map(|ptr| unsafe { |
|
542 &*(ptr.as_ptr() as *const T).add(index.get() as usize - 1) |
|
543 }) |
|
544 } |
|
545 _ => None, |
533 } |
546 } |
534 } |
547 } |
535 |
548 |
536 pub fn iter<T: TypeIter + 'static>(&mut self) -> DataIterator<T> { |
549 pub fn iter<T: TypeIter + 'static>(&mut self) -> DataIterator<T> { |
537 let mut arg_types = Vec::with_capacity(64); |
550 let mut arg_types = Vec::with_capacity(64); |
612 |
625 |
613 #[derive(Clone)] |
626 #[derive(Clone)] |
614 struct Tag; |
627 struct Tag; |
615 |
628 |
616 #[test] |
629 #[test] |
|
630 fn direct_access() { |
|
631 let mut manager = GearDataManager::new(); |
|
632 manager.register::<Datum>(); |
|
633 for i in 1..=5 { |
|
634 manager.add(GearId::new(i as u16).unwrap(), &Datum { value: i * i }); |
|
635 } |
|
636 |
|
637 for i in 1..=5 { |
|
638 assert_eq!( |
|
639 manager |
|
640 .get::<Datum>(GearId::new(i as u16).unwrap()) |
|
641 .unwrap() |
|
642 .value, |
|
643 i * i |
|
644 ); |
|
645 } |
|
646 } |
|
647 |
|
648 #[test] |
617 fn single_component_iteration() { |
649 fn single_component_iteration() { |
618 let mut manager = GearDataManager::new(); |
650 let mut manager = GearDataManager::new(); |
619 manager.register::<Datum>(); |
651 manager.register::<Datum>(); |
620 for i in 1..=5 { |
652 for i in 1..=5 { |
621 manager.add(GearId::new(i as u16).unwrap(), &Datum { value: i }); |
653 manager.add(GearId::new(i as u16).unwrap(), &Datum { value: i }); |