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
77
78
79
80
81
82
83
84
85
86
87
88
89
use sanakirja::{Representable, Alignment};
use std;

pub const INODE_SIZE: usize = 8;
/// A unique identifier for files or directories in the actual
/// file system, to map "files from the graph" to real files.
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct Inode([u8; INODE_SIZE]);
/// The `Inode` representing the root of the repository (on the
/// actual file system).
pub const ROOT_INODE: Inode = Inode([0; INODE_SIZE]);
impl std::ops::Deref for Inode {
    type Target = [u8];
    fn deref(&self) -> &[u8] {
        &self.0
    }
}
impl std::ops::DerefMut for Inode {
    fn deref_mut(&mut self) -> &mut [u8] {
        &mut self.0
    }
}

use hex::{ToHex};
impl std::fmt::Debug for Inode {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(fmt, "Inode({})", self.0.to_hex())
    }
}
impl Inode {

    pub fn is_root(&self) -> bool {
        *self == ROOT_INODE
    }

    /// Decode an inode from its hexadecimal representation.
    pub fn from_hex(hex: &str) -> Option<Inode> {
        let mut i = Inode([0; INODE_SIZE]);
        if super::from_hex(hex, &mut i) {
            Some(i)
        } else {
            None
        }
    }

    #[doc(hidden)]
    pub fn to_unsafe(&self) -> UnsafeInode {
        UnsafeInode(self)
    }

    #[doc(hidden)]
    pub unsafe fn from_unsafe<'a>(p: UnsafeInode) -> &'a Self {
        &*p.0
    }
}

#[derive(Clone, Copy)]
pub struct UnsafeInode(*const Inode);
impl std::fmt::Debug for UnsafeInode {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        unsafe {
            Inode::from_unsafe(*self).fmt(fmt)
        }
    }
}

impl Representable for UnsafeInode {
    fn alignment() -> Alignment {
        Alignment::B1
    }
    fn onpage_size(&self) -> u16 {
        std::mem::size_of::<Inode>() as u16
    }
    unsafe fn write_value(&self, p: *mut u8) {
        trace!("write_value {:?}", p);
        std::ptr::copy(self.0, p as *mut Inode, 1)
    }
    unsafe fn read_value(p: *const u8) -> Self {
        trace!("read_value {:?}", p);
        UnsafeInode(p as *const Inode)
    }
    unsafe fn cmp_value<T>(&self, _: &T, x: Self) -> std::cmp::Ordering {
        let a: &Inode = &*self.0;
        let b: &Inode = &*x.0;
        a.cmp(b)
    }
    type PageOffsets = std::iter::Empty<u64>;
    fn page_offsets(&self) -> Self::PageOffsets { std::iter::empty() }
}