nut/bucket/
elemref.rs

1use either::Either;
2use std::ops::Deref;
3
4use crate::consts::Flags;
5use crate::node::Node;
6use crate::page::Page;
7
8#[derive(Clone, Debug)]
9pub(crate) struct PageNode(Either<*const Page, Node>);
10
11impl Deref for PageNode {
12    type Target = Either<*const Page, Node>;
13
14    fn deref(&self) -> &Self::Target {
15        &self.0
16    }
17}
18
19impl From<*const Page> for PageNode {
20    fn from(p: *const Page) -> Self {
21        Self(Either::Left(p))
22    }
23}
24
25impl From<Node> for PageNode {
26    fn from(n: Node) -> Self {
27        Self(Either::Right(n))
28    }
29}
30
31impl PageNode {
32    pub(crate) fn get_page(&self) -> &Page {
33        match self.0 {
34            Either::Left(p) => unsafe { &*p },
35            Either::Right(ref _n) => panic!("ElemRef not page"),
36        }
37    }
38
39    pub(crate) fn upgrade(&self) -> Either<&Page, &Node> {
40        match self.0 {
41            Either::Left(p) => unsafe { Either::Left(&*p) },
42            Either::Right(ref n) => Either::Right(n),
43        }
44    }
45
46    pub(crate) fn is_leaf(&self) -> bool {
47        match self.0 {
48            Either::Left(_) => self.get_page().flags == Flags::LEAVES,
49            Either::Right(ref n) => n.is_leaf(),
50        }
51    }
52
53    pub(crate) fn count(&self) -> usize {
54        match self.0 {
55            Either::Left(ref _p) => self.get_page().count as usize,
56            Either::Right(ref n) => n.0.inodes.borrow().len(),
57        }
58    }
59}
60
61/// Represents a reference to an element on a given page/node.
62#[derive(Clone, Debug)]
63pub(crate) struct ElemRef {
64    pub(crate) el: PageNode,
65    pub(crate) index: usize,
66}
67
68impl Deref for ElemRef {
69    type Target = PageNode;
70
71    fn deref(&self) -> &Self::Target {
72        &self.el
73    }
74}