cranpose_core/
scene_records.rs1use std::num::{NonZeroU16, NonZeroU32};
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
6pub struct RecordId(NonZeroU32);
7
8impl RecordId {
9 pub fn from_index(index: usize) -> Option<Self> {
11 u32::try_from(index)
12 .ok()
13 .and_then(|index| index.checked_add(1))
14 .and_then(NonZeroU32::new)
15 .map(Self)
16 }
17
18 pub fn index(self) -> usize {
20 (self.0.get() - 1) as usize
21 }
22}
23
24#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
27pub struct StoreId(NonZeroU16);
28
29impl StoreId {
30 pub fn new(number: u16) -> Option<Self> {
32 NonZeroU16::new(number).map(Self)
33 }
34
35 pub fn number(self) -> u16 {
37 self.0.get()
38 }
39}
40
41#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
44pub struct RecordRef {
45 pub id: RecordId,
47 pub generation: u32,
49}
50
51#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
54pub struct SceneUpdate(u32);
55
56impl SceneUpdate {
57 #[must_use]
59 pub fn next(self) -> Self {
60 Self(self.0.wrapping_add(1))
61 }
62
63 pub fn is_after(self, other: Self) -> bool {
65 self != other && self.0.wrapping_sub(other.0) < 1 << 31
66 }
67
68 pub fn since(self, earlier: Self) -> u32 {
70 self.0.wrapping_sub(earlier.0)
71 }
72}
73
74#[cfg(test)]
75#[path = "tests/scene_records_tests.rs"]
76mod tests;