Skip to main content

triblespace_core/patch/
entry.rs

1use super::*;
2
3/// Reference-counted handle to a leaf node in a PATCH trie.
4#[derive(Debug)]
5#[repr(C)]
6pub struct Entry<const KEY_LEN: usize, V = ()> {
7    ptr: NonNull<Leaf<KEY_LEN, V>>,
8}
9
10impl<const KEY_LEN: usize> Entry<KEY_LEN> {
11    /// Creates a new entry with the given key and a unit value.
12    pub fn new(key: &[u8; KEY_LEN]) -> Self {
13        unsafe {
14            let ptr = Leaf::<KEY_LEN, ()>::new(key, ());
15            Self { ptr }
16        }
17    }
18}
19
20impl<const KEY_LEN: usize, V> Entry<KEY_LEN, V> {
21    /// Creates a new entry with the given key and associated value.
22    pub fn with_value(key: &[u8; KEY_LEN], value: V) -> Self {
23        unsafe {
24            let ptr = Leaf::<KEY_LEN, V>::new(key, value);
25            Self { ptr }
26        }
27    }
28
29    /// Returns a reference to the value stored in this entry.
30    pub fn value(&self) -> &V {
31        unsafe { &self.ptr.as_ref().value }
32    }
33
34    pub(super) fn leaf<O: KeySchema<KEY_LEN>>(&self) -> Head<KEY_LEN, O, V> {
35        unsafe { Head::new(0, Leaf::rc_inc(self.ptr)) }
36    }
37}
38
39impl<const KEY_LEN: usize, V> Clone for Entry<KEY_LEN, V> {
40    fn clone(&self) -> Self {
41        unsafe {
42            Self {
43                ptr: Leaf::rc_inc(self.ptr),
44            }
45        }
46    }
47}
48
49impl<const KEY_LEN: usize, V> Drop for Entry<KEY_LEN, V> {
50    fn drop(&mut self) {
51        unsafe {
52            Leaf::rc_dec(self.ptr);
53        }
54    }
55}