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
use std::{
borrow::{Borrow, BorrowMut},
collections::BTreeMap,
ops::{ControlFlow, Deref, DerefMut},
};
use serde::{Deserialize, Serialize};
pub mod fs;
pub mod parser;
pub mod writer;
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CRCTreeNode<D> {
pub crc: u32,
pub left: i32,
pub right: i32,
pub data: D,
}
impl<D> Borrow<D> for CRCTreeNode<D> {
fn borrow(&self) -> &D {
&self.data
}
}
impl<D> BorrowMut<D> for CRCTreeNode<D> {
fn borrow_mut(&mut self) -> &mut D {
&mut self.data
}
}
impl<D> Deref for CRCTreeNode<D> {
type Target = D;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<D> DerefMut for CRCTreeNode<D> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data
}
}
pub type CRCTree<T> = BTreeMap<u32, T>;
pub trait CRCTreeVisitor<T> {
type Break;
fn visit(&mut self, crc: u32, data: T) -> ControlFlow<Self::Break>;
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct CRCTreeCollector<T> {
inner: CRCTree<T>,
}
impl<T> CRCTreeCollector<T> {
pub fn new() -> Self {
Self {
inner: CRCTree::new(),
}
}
pub fn into_inner(self) -> CRCTree<T> {
self.inner
}
}
impl<T> CRCTreeVisitor<T> for CRCTreeCollector<T> {
type Break = ();
fn visit(&mut self, crc: u32, data: T) -> ControlFlow<Self::Break> {
self.inner.insert(crc, data);
ControlFlow::Continue(())
}
}