slabbable_validation/
lib.rs

1#![warn(
2    clippy::unwrap_used,
3    missing_docs,
4    rust_2018_idioms,
5    unused_lifetimes,
6    unused_qualifications
7)]
8#![allow(clippy::single_match, rustdoc::bare_urls)]
9#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
10#![doc = include_str!("../README.md")]
11
12#[cfg(test)]
13mod test {
14    use slabbable::Slabbable;
15
16    #[repr(packed, C)]
17    #[derive(Debug, Clone, PartialEq)]
18    struct SomeCStruct {
19        forever: u8,
20        whatever: u16,
21        yet_another: u32,
22    }
23
24    mod slabbable_selected {
25        use super::*;
26        use ::slabbable_impl_selector::SelectedSlab;
27
28        #[inline]
29        fn selected_impl<T: Clone + core::fmt::Debug>(cap: usize) -> SelectedSlab<T> {
30            SelectedSlab::<T>::with_fixed_capacity(cap).unwrap()
31        }
32
33        #[test]
34        fn selected_1_3() {
35            let mut imp = selected_impl::<SomeCStruct>(5);
36            _1_3_impl_stable_memory_init(&mut imp, 5);
37        }
38
39        #[test]
40        fn selected_2_3() {
41            let mut imp = selected_impl::<SomeCStruct>(5);
42            _2_3_impl_reserve_re_usable(&mut imp, 5);
43        }
44    }
45
46    // Fill to capacity and check the stored item did not move address
47    fn _1_3_impl_stable_memory_init<ImplT, Slabber>(impl_ut: &mut ImplT, cap: usize)
48    where
49        ImplT: core::fmt::Debug + Slabbable<Slabber, SomeCStruct>,
50        <ImplT as Slabbable<Slabber, SomeCStruct>>::Error: core::fmt::Debug,
51        Slabber: core::fmt::Debug,
52    {
53        assert!(cap > 0);
54        let mut ptrs_chk = Vec::with_capacity(cap);
55        for _z in 0..cap {
56            let slot = impl_ut
57                .take_next_with(SomeCStruct {
58                    forever: 0,
59                    whatever: 0,
60                    yet_another: 0,
61                })
62                .unwrap();
63            let g = impl_ut.slot_get_ref(slot).unwrap().unwrap();
64            let ptr = std::ptr::addr_of!(*g);
65            ptrs_chk.push((slot, ptr));
66        }
67
68        for (slot, ptr) in ptrs_chk {
69            let chk = impl_ut.slot_get_ref(slot).unwrap().unwrap();
70            let chk_ptr = std::ptr::addr_of!(*chk);
71            assert_eq!(ptr, chk_ptr);
72        }
73    }
74
75    // Reserve to capacity and check we are able to take all
76    fn _2_3_impl_reserve_re_usable<ImplT, Slabber>(impl_ut: &mut ImplT, cap: usize)
77    where
78        ImplT: core::fmt::Debug + Slabbable<Slabber, SomeCStruct>,
79        <ImplT as Slabbable<Slabber, SomeCStruct>>::Error: core::fmt::Debug,
80        Slabber: core::fmt::Debug,
81    {
82        assert!(cap > 0);
83        let mut reserve_chk = Vec::with_capacity(cap);
84        for _z in 0..cap {
85            reserve_chk.push(impl_ut.reserve_next().unwrap());
86        }
87
88        let mut c = 0;
89        let mut taken_chk = Vec::with_capacity(cap);
90        for reserved in reserve_chk {
91            c += 1;
92            let st = SomeCStruct {
93                forever: 0,
94                whatever: 0,
95                yet_another: c as u32,
96            };
97            taken_chk.push((reserved.id(), c));
98            impl_ut.take_reserved_with(reserved, st).unwrap();
99        }
100        for (taken_id, counter) in taken_chk {
101            let chk_r = impl_ut.slot_get_ref(taken_id).unwrap().unwrap();
102            let chk_st = SomeCStruct {
103                forever: 0,
104                whatever: 0,
105                yet_another: counter as u32,
106            };
107            assert_eq!(chk_st, *chk_r);
108        }
109    }
110}