libpijul_compat/backend/
patch_id.rs1use bs58;
2use sanakirja::{Alignment, Representable};
3use std;
4
5pub const PATCH_ID_SIZE: usize = 8;
7pub const ROOT_PATCH_ID: PatchId = PatchId([0; PATCH_ID_SIZE]);
8
9#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)]
12pub struct PatchId([u8; PATCH_ID_SIZE]);
13
14impl std::fmt::Debug for PatchId {
15 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
16 write!(fmt, "PatchId({})", self.to_base58())
17 }
18}
19
20use hex::ToHex;
21use std::fmt::Write;
22impl ToHex for PatchId {
23 fn write_hex<W: Write>(&self, w: &mut W) -> std::fmt::Result {
24 self.0.write_hex(w)
25 }
26 fn write_hex_upper<W: Write>(&self, w: &mut W) -> std::fmt::Result {
27 self.0.write_hex_upper(w)
28 }
29}
30impl PatchId {
31 pub fn new() -> Self {
33 PatchId([0; PATCH_ID_SIZE])
34 }
35 pub fn to_base58(&self) -> String {
37 bs58::encode(&self.0).into_string()
38 }
39 pub fn from_base58(s: &str) -> Option<Self> {
41 let mut p = PatchId::new();
42 if bs58::decode(s).into(&mut p.0).is_ok() {
43 Some(p)
44 } else {
45 None
46 }
47 }
48}
49
50impl std::ops::Deref for PatchId {
51 type Target = [u8];
52 fn deref(&self) -> &Self::Target {
53 &self.0
54 }
55}
56impl std::ops::DerefMut for PatchId {
57 fn deref_mut(&mut self) -> &mut Self::Target {
58 &mut self.0
59 }
60}
61
62impl Representable for PatchId {
63 fn alignment() -> Alignment {
64 Alignment::B1
65 }
66 fn onpage_size(&self) -> u16 {
67 8
68 }
69 unsafe fn write_value(&self, p: *mut u8) {
70 trace!("write_value {:?}", p);
71 std::ptr::copy(self.0.as_ptr(), p, PATCH_ID_SIZE)
72 }
73 unsafe fn read_value(p: *const u8) -> Self {
74 trace!("read_value {:?}", p);
75 let mut patch_id: PatchId = std::mem::uninitialized();
76 std::ptr::copy(p, patch_id.0.as_mut_ptr(), 8);
77 patch_id
78 }
79 unsafe fn cmp_value<T>(&self, _: &T, x: Self) -> std::cmp::Ordering {
80 self.0.cmp(&x.0)
81 }
82 type PageOffsets = std::iter::Empty<u64>;
83 fn page_offsets(&self) -> Self::PageOffsets {
84 std::iter::empty()
85 }
86}