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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
pub use save_state_derive::*;

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use paste::paste;
use std::convert::From;
use std::io::{
    self, Cursor, Error as ioError, ErrorKind as ioErrorKind, Read, Result as ioResult, Write,
};

type CiboriumSerIoError = ciborium::ser::Error<io::Error>;
type CiboriumDeIoError = ciborium::de::Error<io::Error>;
pub type Result<T> = std::result::Result<T, Error>;

pub fn serialize_into<W, T>(writer: W, value: &T) -> Result<()>
where
    W: std::io::Write,
    T: serde::Serialize,
{
    ciborium::ser::into_writer(value, writer).map_err(|e| e.into())
}

pub fn deserialize_from<R, T>(reader: R) -> Result<T>
where
    R: std::io::Read,
    T: serde::de::DeserializeOwned,
{
    ciborium::de::from_reader(reader).map_err(|e| e.into())
}

pub fn serialized_size<T>(value: &T) -> Result<u64>
where
    T: serde::Serialize,
{
    let mut counter = Counter::default();
    ciborium::ser::into_writer(value, &mut counter)?;
    Ok(counter.counter)
}

/// a simple help that implements `io::Write`, which helps get the size of
/// a Savable object
#[derive(Default)]
struct Counter {
    counter: u64,
}

impl Counter {
    #[inline]
    fn add(&mut self, c: usize) -> ioResult<()> {
        // for some reason, using `checked_add` is exponentially slower, this is good
        let (counter, overflow) = self.counter.overflowing_add(c as u64);
        self.counter = counter;

        if overflow {
            Err(ioError::new(
                ioErrorKind::InvalidInput,
                "write length exceed u64 limit",
            ))
        } else {
            Ok(())
        }
    }
}

impl Write for Counter {
    #[inline]
    fn write(&mut self, buf: &[u8]) -> ioResult<usize> {
        self.add(buf.len())?;
        Ok(buf.len())
    }

    #[inline]
    fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> ioResult<usize> {
        let len = bufs.iter().map(|b| b.len()).sum();
        self.add(len)?;
        Ok(len)
    }

    #[inline]
    fn write_all(&mut self, buf: &[u8]) -> ioResult<()> {
        self.add(buf.len())?;
        Ok(())
    }

    #[inline]
    fn flush(&mut self) -> ioResult<()> {
        Ok(())
    }
}

pub trait Savable {
    fn save<W: Write>(&self, writer: &mut W) -> Result<()>;
    fn load<R: Read>(&mut self, reader: &mut R) -> Result<()>;
    /// The size of the object if saved now, note that this might change, for example
    /// due to the length of string objects or data inside the object.
    #[inline]
    fn save_size(&self) -> Result<u64> {
        let mut counter = Counter::default();
        self.save(&mut counter)?;
        Ok(counter.counter)
    }
}

pub fn save_object<T: Savable>(object: &T) -> Result<Vec<u8>> {
    let mut result = Vec::new();
    object.save(&mut result)?;

    Ok(result)
}

pub fn load_object<T: Savable>(object: &mut T, data: &[u8]) -> Result<()> {
    let mut cursor = Cursor::new(data);
    object.load(&mut cursor)?;

    let (remaining_data_len, overflow) = (data.len() as u64).overflowing_sub(cursor.position());
    assert!(!overflow);

    if remaining_data_len > 0 {
        Err(Error::TrailingData(remaining_data_len))
    } else {
        Ok(())
    }
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("Io Eror: {0}")]
    IoError(ioError),
    #[error("Cobr Serialization Error: {0}")]
    CiboriumSerialization(CiboriumSerIoError),
    #[error("Cobr Deserialization Error: {0}")]
    CiboriumDeserialization(CiboriumDeIoError),
    #[error("After loading an object, some data still remained ({0} bytes)")]
    TrailingData(u64),
    #[error("Enum could not be loaded correctly due to corrupted data ({0})")]
    InvalidEnumVariant(usize),
}

impl From<ioError> for Error {
    fn from(e: ioError) -> Self {
        Error::IoError(e)
    }
}

impl From<CiboriumSerIoError> for Error {
    fn from(e: CiboriumSerIoError) -> Self {
        Self::CiboriumSerialization(e)
    }
}

impl From<CiboriumDeIoError> for Error {
    fn from(e: CiboriumDeIoError) -> Self {
        Self::CiboriumDeserialization(e)
    }
}

macro_rules! impl_primitive {
    ($struct_name: ident $(, $g: tt)? ) => {
        impl Savable for $struct_name {
            #[inline]
            fn save<W: ::std::io::Write>(&self, writer: &mut W) -> Result<()> {
                paste!(writer.[<write_ $struct_name>]$($g<LittleEndian>)?(*self)?);
                Ok(())
            }

            #[inline]
            fn load<R: ::std::io::Read>(&mut self, reader: &mut R) -> Result<()> {
                *self = paste!(reader.[<read_ $struct_name>]$($g<LittleEndian>)?()?);
                Ok(())
            }

            #[inline]
            fn save_size(&self) -> Result<u64> {
               Ok(::std::mem::size_of::<Self>() as u64)
            }
        }
    };
}

// this is used here to implement some std types
macro_rules! impl_savable_with_serde {
    ($struct_name: ident $(<$($generics: ident),+>)?) => {
        impl $(<$($generics: serde::Serialize + serde::de::DeserializeOwned),+>)? Savable for $struct_name $(<$($generics),+>)?{
            #[inline]
            fn save<W: ::std::io::Write>(&self, writer: &mut W) -> Result<()> {
                serialize_into(writer, self)?;
                Ok(())
            }

            #[inline]
            fn load<R: ::std::io::Read>(&mut self, reader: &mut R) -> Result<()> {
                let obj = deserialize_from(reader)?;
                let _ = ::std::mem::replace(self, obj);
                Ok(())
            }
        }
    };
}

macro_rules! impl_for_tuple {
    ($($id: tt $tuple_element: ident),+) => {
        impl<$($tuple_element),+> Savable for ($($tuple_element),+)
        where $($tuple_element: Savable),+
        {
            #[inline]
            fn save<W: ::std::io::Write>(&self, mut writer: &mut W) -> Result<()> {
                $(<$tuple_element as Savable>::save(&self.$id, &mut writer)?;)+
                Ok(())
            }

            #[inline]
            fn load<R: ::std::io::Read>(&mut self, mut reader: &mut R) -> Result<()> {
                $(<$tuple_element as Savable>::load(&mut self.$id, &mut reader)?;)+
                Ok(())
            }
        }
    };
}

impl_primitive!(u8);
impl_primitive!(u16, ::);
impl_primitive!(u32, ::);
impl_primitive!(u64, ::);
impl_primitive!(i8);
impl_primitive!(i16, ::);
impl_primitive!(i32, ::);
impl_primitive!(i64, ::);
impl_primitive!(f32, ::);
impl_primitive!(f64, ::);
impl_savable_with_serde!(bool);
impl_savable_with_serde!(char);
impl_savable_with_serde!(String);
impl_savable_with_serde!(Vec<T>);

impl_for_tuple!(0 A0, 1 A1);
impl_for_tuple!(0 A0, 1 A1, 2 A2);
impl_for_tuple!(0 A0, 1 A1, 2 A2, 3 A3);
impl_for_tuple!(0 A0, 1 A1, 2 A2, 3 A3, 4 A4);
impl_for_tuple!(0 A0, 1 A1, 2 A2, 3 A3, 4 A4, 5 A5);
impl_for_tuple!(0 A0, 1 A1, 2 A2, 3 A3, 4 A4, 5 A5, 6 A6);
impl_for_tuple!(0 A0, 1 A1, 2 A2, 3 A3, 4 A4, 5 A5, 6 A6, 7 A7);
impl_for_tuple!(0 A0, 1 A1, 2 A2, 3 A3, 4 A4, 5 A5, 6 A6, 7 A7, 8 A8);

impl Savable for usize {
    fn save<W: ::std::io::Write>(&self, writer: &mut W) -> Result<()> {
        writer.write_u64::<LittleEndian>(*self as u64)?;
        Ok(())
    }

    fn load<R: ::std::io::Read>(&mut self, reader: &mut R) -> Result<()> {
        *self = reader.read_u64::<LittleEndian>()? as usize;
        Ok(())
    }

    fn save_size(&self) -> Result<u64> {
        Ok(::std::mem::size_of::<u64>() as u64)
    }
}

impl Savable for isize {
    fn save<W: ::std::io::Write>(&self, writer: &mut W) -> Result<()> {
        writer.write_i64::<LittleEndian>(*self as i64)?;
        Ok(())
    }

    fn load<R: ::std::io::Read>(&mut self, reader: &mut R) -> Result<()> {
        *self = reader.read_i64::<LittleEndian>()? as isize;
        Ok(())
    }

    fn save_size(&self) -> Result<u64> {
        Ok(::std::mem::size_of::<i64>() as u64)
    }
}

// TODO: wait for `min_specialization` feature and implement
//  `u8` separatly as it would be faster
impl<T, const N: usize> Savable for [T; N]
where
    T: Savable,
{
    fn save<W: ::std::io::Write>(&self, mut writer: &mut W) -> Result<()> {
        for element in self {
            element.save(&mut writer)?;
        }
        Ok(())
    }

    fn load<R: ::std::io::Read>(&mut self, mut reader: &mut R) -> Result<()> {
        for element in self {
            element.load(&mut reader)?;
        }
        Ok(())
    }
}

impl<T> Savable for Option<T>
where
    T: Savable + Default,
{
    fn save<W: Write>(&self, mut writer: &mut W) -> Result<()> {
        match self {
            Some(s) => {
                true.save(&mut writer)?;
                s.save(&mut writer)?;
            }
            None => false.save(&mut writer)?,
        }
        Ok(())
    }

    fn load<R: Read>(&mut self, mut reader: &mut R) -> Result<()> {
        let mut value = false;
        value.load(&mut reader)?;

        if value {
            match self {
                Some(s) => {
                    s.load(&mut reader)?;
                }
                None => {
                    let mut s = T::default();
                    s.load(&mut reader)?;
                    self.replace(s);
                }
            }
        } else {
            *self = None;
        }

        Ok(())
    }
}

impl<T> Savable for std::marker::PhantomData<T> {
    fn save<W: Write>(&self, _writer: &mut W) -> Result<()> {
        Ok(())
    }

    fn load<R: Read>(&mut self, _reader: &mut R) -> Result<()> {
        Ok(())
    }
}

impl Savable for () {
    fn save<W: Write>(&self, _writer: &mut W) -> Result<()> {
        Ok(())
    }

    fn load<R: Read>(&mut self, _reader: &mut R) -> Result<()> {
        Ok(())
    }
}