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
90
91
92
93
use sanakirja::{Representable, Alignment};
use std;
use bs58;
pub const PATCH_ID_SIZE: usize = 8;
pub const ROOT_PATCH_ID: PatchId = PatchId([0; PATCH_ID_SIZE]);
pub const P_ROOT_PATCH_ID: &'static PatchId = &PatchId([0; PATCH_ID_SIZE]);
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)]
pub struct PatchId([u8; PATCH_ID_SIZE]);
impl std::fmt::Debug for PatchId {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(fmt, "PatchId({})", self.to_base58())
}
}
use hex::ToHex;
impl ToHex for PatchId {
fn to_hex(&self) -> String {
self.0.to_hex()
}
}
impl PatchId {
pub fn new() -> Self {
PatchId([0; PATCH_ID_SIZE])
}
pub fn to_base58(&self) -> String {
bs58::encode(&self.0).into_string()
}
pub fn from_base58(s: &str) -> Option<Self> {
let mut p = PatchId::new();
if bs58::decode(s).into(&mut p.0).is_ok() {
Some(p)
} else {
None
}
}
}
impl std::ops::Deref for PatchId {
type Target = [u8];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for PatchId {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[derive(Debug, Clone, Copy)]
pub struct UnsafePatchId(*const PatchId);
impl Representable for UnsafePatchId {
fn alignment() -> Alignment {
Alignment::B1
}
fn onpage_size(&self) -> u16 {
std::mem::size_of::<PatchId>() as u16
}
unsafe fn write_value(&self, p: *mut u8) {
trace!("write_value {:?}", p);
std::ptr::copy(self.0, p as *mut PatchId, 1)
}
unsafe fn read_value(p: *const u8) -> Self {
trace!("read_value {:?}", p);
UnsafePatchId(p as *const PatchId)
}
unsafe fn cmp_value<T>(&self, _: &T, x: Self) -> std::cmp::Ordering {
let a: &PatchId = &*self.0;
let b: &PatchId = PatchId::from_unsafe(x);
a.cmp(b)
}
type PageOffsets = std::iter::Empty<u64>;
fn page_offsets(&self) -> Self::PageOffsets { std::iter::empty() }
}
impl PatchId {
#[doc(hidden)]
pub fn to_unsafe(&self) -> UnsafePatchId {
UnsafePatchId(self)
}
#[doc(hidden)]
pub unsafe fn from_unsafe<'a>(p: UnsafePatchId) -> &'a PatchId {
&*p.0
}
}