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
//! Read/write set.

/// A coarsened key prefix that represents any key that starts with
/// this prefix.
#[derive(Clone, Debug, Default, PartialEq, Eq, cbor::Encode, cbor::Decode)]
#[cbor(transparent)]
pub struct CoarsenedKey(pub Vec<u8>);

impl AsRef<[u8]> for CoarsenedKey {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl From<CoarsenedKey> for Vec<u8> {
    fn from(val: CoarsenedKey) -> Self {
        val.0
    }
}

impl From<Vec<u8>> for CoarsenedKey {
    fn from(v: Vec<u8>) -> CoarsenedKey {
        CoarsenedKey(v)
    }
}

/// A set of coarsened keys.
pub type CoarsenedSet = Vec<CoarsenedKey>;

/// A read/write set.
#[derive(Clone, Debug, Default, PartialEq, Eq, cbor::Encode, cbor::Decode)]
pub struct ReadWriteSet {
    /// Size of the key prefixes (in bytes) used for coarsening the keys.
    pub granularity: u16,
    /// The read set.
    pub read_set: CoarsenedSet,
    /// The write set.
    pub write_set: CoarsenedSet,
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_serialization() {
        let rw_set = ReadWriteSet {
            granularity: 3,
            read_set: vec![b"foo".to_vec().into(), b"bar".to_vec().into()],
            write_set: vec![b"moo".to_vec().into()],
        };

        let enc = cbor::to_vec(rw_set.clone());

        let dec_rw_set: ReadWriteSet = cbor::from_slice(&enc).unwrap();
        assert_eq!(rw_set, dec_rw_set, "serialization should round-trip");
    }
}