Skip to main content

SerdeBinding

Struct SerdeBinding 

Source
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:

  1. 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.
  2. For schemas that need to evolve, use the DPL (noxu-persist) which has explicit @KeyField / @SecondaryKey annotation-driven evolution; see docs/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>

Source

pub fn new() -> Self

Creates a new serde-based binding.

Trait Implementations§

Source§

impl<T> Clone for SerdeBinding<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for SerdeBinding<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for SerdeBinding<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Serialize + DeserializeOwned> EntryBinding<T> for SerdeBinding<T>

Source§

fn entry_to_object(&self, entry: &DatabaseEntry) -> Result<T>

Converts a DatabaseEntry to an object. Read more
Source§

fn object_to_entry(&self, object: &T, entry: &mut DatabaseEntry) -> Result<()>

Converts an object to a DatabaseEntry. Read more

Auto 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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.