Skip to main content

cranpose_core/
node_id_cell.rs

1use std::cell::Cell;
2
3use crate::{NodeId, PackedNodeId};
4
5/// A node id or none in one word, where `Cell<Option<NodeId>>` takes two: a
6/// layout node keeps three, and the 24 bytes kept it out of jemalloc's
7/// 640-byte size class.
8pub struct NodeIdCell(Cell<PackedNodeId>);
9
10impl NodeIdCell {
11    /// A cell holding `id`.
12    pub const fn new(id: Option<NodeId>) -> Self {
13        Self(Cell::new(PackedNodeId::new(id)))
14    }
15
16    /// The id the cell holds.
17    #[inline]
18    pub fn get(&self) -> Option<NodeId> {
19        self.0.get().get()
20    }
21
22    /// Puts `id` in the cell.
23    pub fn set(&self, id: Option<NodeId>) {
24        self.0.set(PackedNodeId::from(id));
25    }
26
27    /// Puts `id` in the cell and returns the id it held.
28    pub fn replace(&self, id: Option<NodeId>) -> Option<NodeId> {
29        let previous = self.get();
30        self.set(id);
31        previous
32    }
33}