cranpose_core/
node_id_cell.rs1use std::cell::Cell;
2
3use crate::NodeId;
4
5pub struct NodeIdCell(Cell<NodeId>);
9
10const NONE: NodeId = NodeId(usize::MAX);
13
14impl NodeIdCell {
15 pub const fn new(id: Option<NodeId>) -> Self {
17 Self(Cell::new(match id {
18 Some(id) => id,
19 None => NONE,
20 }))
21 }
22
23 pub fn get(&self) -> Option<NodeId> {
25 Some(self.0.get()).filter(|&id| id != NONE)
28 }
29
30 pub fn set(&self, id: Option<NodeId>) {
32 debug_assert_ne!(id, Some(NONE), "no node takes the none id");
33 self.0.set(id.unwrap_or(NONE));
34 }
35
36 pub fn replace(&self, id: Option<NodeId>) -> Option<NodeId> {
38 let previous = self.get();
39 self.set(id);
40 previous
41 }
42}