binver/traits.rs
1use crate::{ReadResult, Version, WriteResult};
2
3/// The main trait of this crate that is used for (de)serialization
4pub trait Serializable<'a>: Sized {
5 /// Serialize the current object into the given writer.
6 fn serialize(&self, writer: &mut dyn Writer) -> WriteResult;
7
8 /// Attempt to deserialize this object from the given reader.
9 fn deserialize(reader: &mut dyn Reader<'a>) -> ReadResult<Self>;
10}
11
12/// Generic writer
13pub trait Writer {
14 /// Write all bytes from the given slice to the writer.
15 /// This function must block until all bytes have been written.
16 fn write(&mut self, bytes: &[u8]) -> WriteResult;
17}
18
19/// Generic reader
20pub trait Reader<'a> {
21 /// Return the version of the format being read.
22 fn version(&self) -> Version;
23 /// Fill the given slice with bytes. All bytes must be read.
24 fn read(&mut self, bytes: &mut [u8]) -> ReadResult;
25 /// Read a slice of length `len` from the reader. Must be return exactly the amount of bytes being requested.
26 ///
27 /// If the reader does not have it's own internal buffer (e.g. `std::fs::File`) `ReadError::ReaderNotPersistent` should be returned.
28 fn read_slice(&mut self, len: usize) -> ReadResult<&'a [u8]>;
29}