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
//! Items related to (de)serializing models.
use crate::{
    model::{Model, StoredModel},
    Result,
};

/// (De)serializes [`Model`]s in the form of a slice of `u8` bytes.
/// When a [`Model`] is serialized, it is serialized along with its version, via a [`StoredModel`]
/// struct.
///
/// Both the model itself *and* its version must be serialized and deserialized.
pub trait Serializer {
    /// Serializes a [`Model`] and its version into bytes.
    ///
    /// **NOTE**: The model's version *must* be serialized as part of the process.
    fn to_bytes<M, R>(&self, model: &M) -> Result<R>
    where
        M: Model,
        R: AsRef<[u8]>;

    /// Deserializes a [`Model`] and its version from bytes.
    ///
    /// **NOTE**: The model's version *must* be deserialized as part of the process.
    fn from_bytes<M, R>(&self, bytes: R) -> Result<StoredModel<M>>
    where
        M: Model,
        R: AsRef<[u8]>;
}