noxu_bind/tuple/tuple_binding.rs
1//! TupleBinding trait for type-specific tuple serialization.
2//!
3
4use noxu_db::DatabaseEntry;
5
6use crate::entry_binding::EntryBinding;
7use crate::error::Result;
8use crate::tuple::tuple_input::TupleInput;
9use crate::tuple::tuple_output::TupleOutput;
10
11/// A binding that uses `TupleInput` and `TupleOutput` to serialize/deserialize
12/// values of type `T`.
13///
14/// Implementors define `tuple_to_object` and `object_to_tuple` to convert
15/// between a type and its tuple-encoded byte representation.
16///
17///
18pub trait TupleBinding<T>: EntryBinding<T> {
19 /// Creates a `TupleInput` from a `DatabaseEntry`.
20 fn entry_to_input(entry: &DatabaseEntry) -> TupleInput {
21 TupleInput::new(entry.data())
22 }
23
24 /// Converts tuple input to an object.
25 ///
26 /// # Errors
27 ///
28 /// Returns `BindError` if the data cannot be deserialized.
29 fn tuple_to_object(&self, input: &mut TupleInput) -> Result<T>;
30
31 /// Converts an object to tuple output.
32 ///
33 /// # Errors
34 ///
35 /// Returns `BindError` if the object cannot be serialized.
36 fn object_to_tuple(
37 &self,
38 object: &T,
39 output: &mut TupleOutput,
40 ) -> Result<()>;
41}