pub struct SerdeBinding<T> { /* private fields */ }Expand description
Binding that uses a compact binary format via serde for serialization.
Any type implementing Serialize + DeserializeOwned can be stored in and
retrieved from database entries using this binding. Each entry is
prefixed with a 2-byte header; see the module docs for the format.
§Schema management caveat
SerdeBinding does not carry a per-record schema descriptor.
The 2-byte header guards against decoding records produced by an
incompatible wire format (BindError::VersionMismatch), but it
cannot detect changes in the Rust struct being serialised:
- Adding a struct field, removing a struct field, or reordering fields silently corrupts records written by an earlier build of the same binary — the deserializer walks the same field list, in the same order, with no field tags to anchor it.
JE’s SerialBinding solved the same problem with a
StoredClassCatalog keyed off a per-record class id; this crate
has no equivalent today. Two concrete mitigations:
- Keep the on-disk struct stable and add fields only via
Option<T>-typed wrappers under a new top-level enum variant that you can match on at the application layer. - For schemas that need to evolve, use the DPL
(
noxu-persist) which has explicit@KeyField/@SecondaryKeyannotation-driven evolution; seedocs/src/collections/entity-persistence.md.
§Examples
ⓘ
use serde::{Serialize, Deserialize};
use noxu_bind::serial::serde_binding::SerdeBinding;
use noxu_bind::entry_binding::EntryBinding;
use noxu_db::DatabaseEntry;
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let binding = SerdeBinding::<Person>::new();
let person = Person { name: "Alice".into(), age: 30 };
let mut entry = DatabaseEntry::new();
binding.object_to_entry(&person, &mut entry).unwrap();
let decoded = binding.entry_to_object(&entry).unwrap();
assert_eq!(decoded, person);Implementations§
Source§impl<T> SerdeBinding<T>
impl<T> SerdeBinding<T>
Trait Implementations§
Source§impl<T> Clone for SerdeBinding<T>
impl<T> Clone for SerdeBinding<T>
Source§impl<T> Debug for SerdeBinding<T>
impl<T> Debug for SerdeBinding<T>
Source§impl<T> Default for SerdeBinding<T>
impl<T> Default for SerdeBinding<T>
Source§impl<T: Serialize + DeserializeOwned> EntryBinding<T> for SerdeBinding<T>
impl<T: Serialize + DeserializeOwned> EntryBinding<T> for SerdeBinding<T>
Source§fn entry_to_object(&self, entry: &DatabaseEntry) -> Result<T>
fn entry_to_object(&self, entry: &DatabaseEntry) -> Result<T>
Converts a
DatabaseEntry to an object. Read moreSource§fn object_to_entry(&self, object: &T, entry: &mut DatabaseEntry) -> Result<()>
fn object_to_entry(&self, object: &T, entry: &mut DatabaseEntry) -> Result<()>
Converts an object to a
DatabaseEntry. Read moreAuto Trait Implementations§
impl<T> Freeze for SerdeBinding<T>
impl<T> RefUnwindSafe for SerdeBinding<T>where
T: RefUnwindSafe,
impl<T> Send for SerdeBinding<T>where
T: Send,
impl<T> Sync for SerdeBinding<T>where
T: Sync,
impl<T> Unpin for SerdeBinding<T>where
T: Unpin,
impl<T> UnsafeUnpin for SerdeBinding<T>
impl<T> UnwindSafe for SerdeBinding<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more