libpijul_compat/backend/
edge.rs

1use super::key::*;
2use super::patch_id::*;
3use sanakirja::*;
4use std;
5
6bitflags! {
7    /// Possible flags of edges.
8    ///
9    /// Possible values are `PSEUDO_EDGE`, `FOLDER_EDGE`,
10    /// `PARENT_EDGE` and `DELETED_EDGE`.
11    #[derive(Serialize, Deserialize)]
12    pub struct EdgeFlags: u8 {
13        /// A pseudo-edge, computed when applying the patch to
14        /// restore connectivity, and/or mark conflicts.
15        const PSEUDO_EDGE = 1;
16        /// An edge encoding file system hierarchy.
17        const FOLDER_EDGE = 2;
18        /// An epsilon-edge, i.e. a "non-transitive" edge used to
19        /// solve conflicts.
20        const EPSILON_EDGE = 4;
21        /// A "reverse" edge (all edges in the graph have a reverse edge).
22        const PARENT_EDGE = 8;
23        /// An edge whose target (if not also `PARENT_EDGE`) or
24        /// source (if also `PARENT_EDGE`) is marked as deleted.
25        const DELETED_EDGE = 16;
26    }
27}
28
29/// The target half of an edge in the repository graph.
30#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)]
31#[repr(packed)]
32pub struct Edge {
33    /// Flags of this edge.
34    pub flag: EdgeFlags,
35    /// Target of this edge.
36    pub dest: Key<PatchId>,
37    /// Patch that introduced this edge (possibly as a
38    /// pseudo-edge, i.e. not explicitly in the patch, but
39    /// computed from it).
40    pub introduced_by: PatchId,
41}
42impl Edge {
43    /// Create an edge with the flags set to `flags`, and other
44    /// parameters to 0.
45    pub fn zero(flag: EdgeFlags) -> Edge {
46        Edge {
47            flag: flag,
48            dest: ROOT_KEY.clone(),
49            introduced_by: ROOT_PATCH_ID.clone(),
50        }
51    }
52    #[doc(hidden)]
53    pub fn to_unsafe(&self) -> UnsafeEdge {
54        UnsafeEdge(self)
55    }
56    #[doc(hidden)]
57    pub unsafe fn from_unsafe<'a>(p: UnsafeEdge) -> &'a Edge {
58        &*p.0
59    }
60}
61
62#[derive(Clone, Copy)]
63pub struct UnsafeEdge(*const Edge);
64
65impl std::fmt::Debug for UnsafeEdge {
66    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
67        unsafe { Edge::from_unsafe(*self).fmt(fmt) }
68    }
69}
70
71impl Representable for UnsafeEdge {
72    fn alignment() -> Alignment {
73        Alignment::B1
74    }
75    fn onpage_size(&self) -> u16 {
76        std::mem::size_of::<Edge>() as u16
77    }
78    unsafe fn write_value(&self, p: *mut u8) {
79        trace!("write_value {:?}", p);
80        std::ptr::copy(self.0, p as *mut Edge, 1)
81    }
82    unsafe fn read_value(p: *const u8) -> Self {
83        trace!("read_value {:?}", p);
84        UnsafeEdge(p as *const Edge)
85    }
86    unsafe fn cmp_value<T>(&self, _: &T, x: Self) -> std::cmp::Ordering {
87        let a: &Edge = &*self.0;
88        let b: &Edge = &*x.0;
89        a.cmp(b)
90    }
91    type PageOffsets = std::iter::Empty<u64>;
92    fn page_offsets(&self) -> Self::PageOffsets {
93        std::iter::empty()
94    }
95}