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
use std::io;
use std::ops::Deref;
use crate::{Associative, ByteHash, Content, Sink, Source};
#[derive(Clone, Debug)]
pub struct MaxKey<K>(K);
pub trait MaxKeyType: Ord + Clone {}
impl<T> MaxKeyType for T where T: Ord + Clone {}
impl<K> Deref for MaxKey<K> {
type Target = K;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<K> Associative for MaxKey<K>
where
K: MaxKeyType,
{
fn op(&mut self, b: &Self) {
if b.0 > self.0 {
self.0 = b.0.clone()
}
}
}
impl<T, K> From<&T> for MaxKey<K>
where
T: AsRef<K>,
K: MaxKeyType,
{
fn from(t: &T) -> Self {
MaxKey(t.as_ref().clone())
}
}
impl<H: ByteHash, K: Content<H>> Content<H> for MaxKey<K>
where
K: MaxKeyType,
{
fn persist(&mut self, sink: &mut Sink<H>) -> io::Result<()> {
self.0.persist(sink)
}
fn restore(source: &mut Source<H>) -> io::Result<Self> {
Ok(MaxKey(K::restore(source)?))
}
}