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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use core::hash::Hash;
use crate::{Canon, DrySink, InvalidEncoding};
pub trait IdBuilder<I>: Default {
fn write_bytes(&mut self, bytes: &[u8]);
fn fin(self) -> I;
}
pub trait Ident:
'static + Default + AsRef<[u8]> + AsMut<[u8]> + Clone + Eq + Copy + Hash + core::fmt::Debug
{
type Builder: IdBuilder<Self>;
}
pub trait Sink<S: Store> {
fn copy_bytes(&mut self, bytes: &[u8]);
fn recur<T: Canon<S>>(&self, t: &T) -> Result<S::Ident, S::Error>;
fn fin(self) -> S::Ident;
}
pub trait Source<S> {
fn read_bytes(&mut self, n: usize) -> &[u8];
fn store(&self) -> &S;
}
pub trait Store: 'static + Clone + Default {
type Ident: Ident;
type Error: From<InvalidEncoding> + core::fmt::Debug;
fn fetch(&self, id: &Self::Ident, into: &mut [u8]) -> Result<(), Self::Error>;
fn get<T: Canon<Self>>(&self, id: &Self::Ident) -> Result<T, Self::Error>;
fn put<T: Canon<Self>>(&self, t: &T) -> Result<Self::Ident, Self::Error>;
fn put_raw(&self, bytes: &[u8]) -> Result<Self::Ident, Self::Error>;
fn ident<T: Canon<Self>>(t: &T) -> Self::Ident {
let mut sink = DrySink::new();
t.write(&mut sink).expect("Drysink cannot fail");
sink.fin().into()
}
}
impl<S> Canon<S> for S
where
S: Store,
{
fn write(&self, _: &mut impl Sink<S>) -> Result<(), S::Error> {
Ok(())
}
fn read(source: &mut impl Source<S>) -> Result<Self, S::Error> {
Ok(source.store().clone())
}
fn encoded_len(&self) -> usize {
0
}
}
pub struct ByteSink<'a, S: Store> {
bytes: &'a mut [u8],
offset: usize,
store: S,
builder: <S::Ident as Ident>::Builder,
}
impl<'a, S> ByteSink<'a, S>
where
S: Store,
{
pub fn new(bytes: &'a mut [u8], store: S) -> Self {
ByteSink {
bytes,
store,
offset: 0,
builder: Default::default(),
}
}
}
impl<'a, S> Sink<S> for ByteSink<'a, S>
where
S: Store,
{
fn copy_bytes(&mut self, bytes: &[u8]) {
self.builder.write_bytes(bytes);
let len = bytes.len();
self.bytes[self.offset..self.offset + len].copy_from_slice(bytes);
self.offset += len;
}
fn recur<T: Canon<S>>(&self, t: &T) -> Result<S::Ident, S::Error> {
self.store.put(t)
}
fn fin(self) -> S::Ident {
self.builder.fin()
}
}
pub struct ByteSource<'a, S> {
bytes: &'a [u8],
offset: usize,
store: S,
}
impl<'a, S> ByteSource<'a, S> {
pub fn new(bytes: &'a [u8], store: S) -> Self {
ByteSource {
bytes,
store,
offset: 0,
}
}
}
impl<'a, S> Source<S> for ByteSource<'a, S>
where
S: Store,
{
fn read_bytes(&mut self, n: usize) -> &[u8] {
let old_offset = self.offset;
self.offset += n;
&self.bytes[old_offset..old_offset + n]
}
fn store(&self) -> &S {
&self.store
}
}