1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::num::{NonZeroU16, NonZeroU32};
/// A record of a scene store: its row, one more than its index, so that an
/// absent record costs no space beside a present one.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct RecordId(NonZeroU32);
impl RecordId {
/// The record at row `index`; `None` past the last row a store can hold.
pub fn from_index(index: usize) -> Option<Self> {
u32::try_from(index)
.ok()
.and_then(|index| index.checked_add(1))
.and_then(NonZeroU32::new)
.map(Self)
}
/// The row of the record.
pub fn index(self) -> usize {
(self.0.get() - 1) as usize
}
}
/// A scene store, one per surface renderer, so a layout node that moves to
/// another window knows which store holds its records.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct StoreId(NonZeroU16);
impl StoreId {
/// The store numbered `number`; `None` for zero.
pub fn new(number: u16) -> Option<Self> {
NonZeroU16::new(number).map(Self)
}
/// The store's number.
pub fn number(self) -> u16 {
self.0.get()
}
}
/// A record held outside a frame: its row and the generation the row had,
/// which tells a holder that the row went to another record since.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct RecordRef {
/// The record's row.
pub id: RecordId,
/// The generation of the row when the holder took it.
pub generation: u32,
}
/// A scene update's number. It wraps: of two numbers less than 2^31 apart,
/// the one reached by counting on from the other is the newer.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct SceneUpdate(u32);
impl SceneUpdate {
/// The update after this one.
#[must_use]
pub fn next(self) -> Self {
Self(self.0.wrapping_add(1))
}
/// Whether this update came after `other`.
pub fn is_after(self, other: Self) -> bool {
self != other && self.0.wrapping_sub(other.0) < 1 << 31
}
/// How many updates came after `earlier` up to this one.
pub fn since(self, earlier: Self) -> u32 {
self.0.wrapping_sub(earlier.0)
}
}
#[cfg(test)]
#[path = "tests/scene_records_tests.rs"]
mod tests;