1 use crate::util::bit;
2 use crate::util::slab::Address;
3 
4 /// An mutation identifier for a slot in the slab. The generation helps prevent
5 /// accessing an entry with an outdated token.
6 #[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
7 pub(crate) struct Generation(usize);
8 
9 impl Generation {
10     pub(crate) const WIDTH: u32 = Address::GENERATION_WIDTH;
11 
12     pub(super) const MAX: usize = bit::mask_for(Address::GENERATION_WIDTH);
13 
14     /// Create a new generation
15     ///
16     /// # Panics
17     ///
18     /// Panics if `value` is greater than max generation.
new(value: usize) -> Generation19     pub(crate) fn new(value: usize) -> Generation {
20         assert!(value <= Self::MAX);
21         Generation(value)
22     }
23 
24     /// Returns the next generation value
next(self) -> Generation25     pub(crate) fn next(self) -> Generation {
26         Generation((self.0 + 1) & Self::MAX)
27     }
28 
to_usize(self) -> usize29     pub(crate) fn to_usize(self) -> usize {
30         self.0
31     }
32 }
33