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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use std::convert::TryInto;

use crate::core::{Diff, Footprint, Serialize};
use crate::error::Error;

/// Empty value, can be used for indexing entries that have a
/// key but no value.
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct Empty;

impl Diff for Empty {
    type D = Empty;

    /// D = C - P
    fn diff(&self, _a: &Self) -> Self::D {
        Empty
    }

    /// P = C - D
    fn merge(&self, _a: &Self::D) -> Self {
        Empty
    }
}

impl Serialize for Empty {
    fn encode(&self, _buf: &mut Vec<u8>) -> usize {
        0
    }

    fn decode(&mut self, _buf: &[u8]) -> Result<usize, Error> {
        Ok(0)
    }
}

impl Footprint for Empty {
    fn footprint(&self) -> isize {
        0
    }
}

/***** Value-Trait implementation for builtin types  *****/

impl Diff for Vec<u8> {
    type D = Vec<u8>;

    /// D = C - P
    fn diff(&self, old: &Self) -> Self::D {
        old.clone()
    }

    /// P = C - D
    fn merge(&self, delta: &Self::D) -> Self {
        delta.clone()
    }
}

// 4 byte header, encoding the length of payload followed by
// the actual payload.
impl Serialize for Vec<u8> {
    fn encode(&self, buf: &mut Vec<u8>) -> usize {
        let hdr1: u32 = self.len().try_into().unwrap();
        let scratch = hdr1.to_be_bytes();

        let mut n = buf.len();
        buf.resize(n + scratch.len() + self.len(), 0);
        buf[n..n + scratch.len()].copy_from_slice(&scratch);
        n += scratch.len();
        buf[n..].copy_from_slice(self);
        scratch.len() + self.len()
    }

    fn decode(&mut self, buf: &[u8]) -> Result<usize, Error> {
        if buf.len() < 4 {
            let msg = format!("bytes decode header {} < 4", buf.len());
            return Err(Error::DecodeFail(msg));
        }
        let len: usize = u32::from_be_bytes(buf[..4].try_into().unwrap())
            .try_into()
            .unwrap();
        if buf.len() < (len + 4) {
            let msg = format!("bytes decode payload {} < {}", buf.len(), len);
            return Err(Error::DecodeFail(msg));
        }
        self.resize(len, 0);
        self.copy_from_slice(&buf[4..len + 4]);
        Ok(len + 4)
    }
}

impl Footprint for Vec<u8> {
    fn footprint(&self) -> isize {
        self.capacity().try_into().unwrap()
    }
}

//-------------------------------------------------------------------

impl Diff for i32 {
    type D = i32;

    /// D = C - P
    fn diff(&self, old: &Self) -> Self::D {
        old.clone()
    }

    /// P = C - D
    fn merge(&self, delta: &Self::D) -> Self {
        delta.clone()
    }
}

impl Serialize for i32 {
    fn encode(&self, buf: &mut Vec<u8>) -> usize {
        let n = buf.len();
        buf.resize(n + 4, 0);
        buf[n..].copy_from_slice(&self.to_be_bytes());
        4
    }

    fn decode(&mut self, buf: &[u8]) -> Result<usize, Error> {
        if buf.len() >= 4 {
            let mut scratch = [0_u8; 4];
            scratch.copy_from_slice(&buf[..4]);
            *self = i32::from_be_bytes(scratch);
            Ok(4)
        } else {
            Err(Error::DecodeFail(format!("i32 encoded len {}", buf.len())))
        }
    }
}

impl Footprint for i32 {
    fn footprint(&self) -> isize {
        0
    }
}

//-------------------------------------------------------------------

impl Diff for i64 {
    type D = i64;

    /// D = C - P
    fn diff(&self, old: &Self) -> Self::D {
        old.clone()
    }

    /// P = C - D
    fn merge(&self, delta: &Self::D) -> Self {
        delta.clone()
    }
}

impl Serialize for i64 {
    fn encode(&self, buf: &mut Vec<u8>) -> usize {
        let n = buf.len();
        buf.resize(n + 8, 0);
        buf[n..].copy_from_slice(&self.to_be_bytes());
        8
    }

    fn decode(&mut self, buf: &[u8]) -> Result<usize, Error> {
        if buf.len() >= 8 {
            let mut scratch = [0_u8; 8];
            scratch.copy_from_slice(&buf[..8]);
            *self = i64::from_be_bytes(scratch);
            Ok(8)
        } else {
            Err(Error::DecodeFail(format!("i64 encoded len {}", buf.len())))
        }
    }
}

impl Footprint for i64 {
    fn footprint(&self) -> isize {
        0
    }
}

//-------------------------------------------------------------------

#[cfg(test)]
#[path = "types_test.rs"]
mod types_test;