rust/hwphysics/src/data.rs
changeset 15376 24a9afbf33c6
parent 15374 0f2fd8d12734
child 15377 7a3ed957cee9
equal deleted inserted replaced
15375:5a934f83d5eb 15376:24a9afbf33c6
    22 
    22 
    23             fn get_types(types: &mut Vec<TypeId>) {
    23             fn get_types(types: &mut Vec<TypeId>) {
    24                 $(types.push(TypeId::of::<$t>()));+
    24                 $(types.push(TypeId::of::<$t>()));+
    25             }
    25             }
    26 
    26 
    27             unsafe fn iter<F: FnMut(Self)>(slices: &[*mut u8], count: usize, mut f: F)
    27             unsafe fn iter<F: FnMut(Self)>(slices: &[*mut u8], count: usize, mut f: F) {
    28             {
       
    29                 for i in 0..count {
    28                 for i in 0..count {
    30                     unsafe {
    29                     unsafe {
    31                         f(($(&*(*slices.get_unchecked($n) as *mut $t).add(i)),+,));
    30                         f(($(&*(*slices.get_unchecked($n) as *mut $t).add(i)),+,));
    32                     }
    31                     }
    33                 }
    32                 }
    41 
    40 
    42             fn get_types(types: &mut Vec<TypeId>) {
    41             fn get_types(types: &mut Vec<TypeId>) {
    43                 $(types.push(TypeId::of::<$t>()));+
    42                 $(types.push(TypeId::of::<$t>()));+
    44             }
    43             }
    45 
    44 
    46             unsafe fn iter<F: FnMut(Self)>(slices: &[*mut u8], count: usize, mut f: F)
    45             unsafe fn iter<F: FnMut(Self)>(slices: &[*mut u8], count: usize, mut f: F) {
    47             {
       
    48                 for i in 0..count {
    46                 for i in 0..count {
    49                     unsafe {
    47                     unsafe {
    50                         f(($(&mut *(*slices.get_unchecked($n) as *mut $t).add(i)),+,));
    48                         f(($(&mut *(*slices.get_unchecked($n) as *mut $t).add(i)),+,));
    51                     }
    49                     }
    52                 }
    50                 }
   325     #[derive(Clone)]
   323     #[derive(Clone)]
   326     struct Datum {
   324     struct Datum {
   327         value: u32,
   325         value: u32,
   328     }
   326     }
   329 
   327 
       
   328     #[derive(Clone)]
       
   329     struct Tag {
       
   330         nothing: u8,
       
   331     }
       
   332 
   330     #[test]
   333     #[test]
   331     fn single_component_iteration() {
   334     fn single_component_iteration() {
   332         assert!(std::mem::size_of::<Datum>() > 0);
       
   333 
       
   334         let mut manager = GearDataManager::new();
   335         let mut manager = GearDataManager::new();
   335         manager.register::<Datum>();
   336         manager.register::<Datum>();
   336         for i in 1..=5 {
   337         for i in 1..=5 {
   337             manager.add(GearId::new(i as u16).unwrap(), &Datum { value: i });
   338             manager.add(GearId::new(i as u16).unwrap(), &Datum { value: i });
   338         }
   339         }
   343 
   344 
   344         manager.iter(|(d,): (&mut Datum,)| d.value += 1);
   345         manager.iter(|(d,): (&mut Datum,)| d.value += 1);
   345         manager.iter(|(d,): (&Datum,)| sum += d.value);
   346         manager.iter(|(d,): (&Datum,)| sum += d.value);
   346         assert_eq!(sum, 35);
   347         assert_eq!(sum, 35);
   347     }
   348     }
   348 }
   349 
       
   350     #[test]
       
   351     fn multiple_component_iteration() {
       
   352         let mut manager = GearDataManager::new();
       
   353         manager.register::<Datum>();
       
   354         manager.register::<Tag>();
       
   355         for i in 1..=10 {
       
   356             let gear_id = GearId::new(i as u16).unwrap();
       
   357             manager.add(gear_id, &Datum { value: i });
       
   358             if i & 1 == 0 {
       
   359                 manager.add(gear_id, &Tag { nothing: 0 });
       
   360             }
       
   361         }
       
   362 
       
   363         let mut sum1 = 0;
       
   364         let mut sum2 = 0;
       
   365         manager.iter(|(d, _): (&Datum, &Tag)| sum1 += d.value);
       
   366         manager.iter(|(_, d): (&Tag, &Datum)| sum2 += d.value);
       
   367         assert_eq!(sum1, 30);
       
   368         assert_eq!(sum2, sum1);
       
   369     }
       
   370 }