Skip to main content

SyncSession

Struct SyncSession 

Source
pub struct SyncSession<T: DeltaPack + Clone> { /* private fields */ }
Expand description

A stateful handle over a one-way state-sync session between two endpoints.

Each endpoint holds a SyncSession representing the current shared view of the peer’s state. The sender calls encode to produce bytes; the receiver calls decode to apply them. Both sides converge on the same view as long as messages are delivered in order.

The type handles the “first call is full encode, subsequent are diffs” distinction internally. It also ensures the sender’s view always matches what the peer reconstructs — even when the user’s state has been mutated or reordered in ways that would break a raw encode_diff call.

§Example

let mut session = SyncSession::<GameState>::new();

// Sender
let bytes = session.encode(&state);

// Receiver
let state = session.decode(&bytes);

Implementations§

Source§

impl<T: DeltaPack + Clone> SyncSession<T>

Source

pub fn new() -> Self

Create a new session with no view yet.

Source

pub fn encode(&mut self, state: &T) -> Vec<u8>

Produce bytes to send to the peer. First call emits a full encode; subsequent calls emit a diff against the current view. Either way, the internal view is updated to match what the peer will hold after applying the returned bytes.

Source

pub fn decode(&mut self, bytes: &[u8]) -> &T

Apply bytes received from the peer. First call expects a full encode; subsequent calls expect a diff. Returns a reference to the updated view.

Source

pub fn current(&self) -> Option<&T>

The current view, or None if neither encode nor decode has been called.

Trait Implementations§

Source§

impl<T: DeltaPack + Clone> Default for SyncSession<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> Freeze for SyncSession<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for SyncSession<T>
where T: RefUnwindSafe,

§

impl<T> Send for SyncSession<T>
where T: Send,

§

impl<T> Sync for SyncSession<T>
where T: Sync,

§

impl<T> Unpin for SyncSession<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for SyncSession<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for SyncSession<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> 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, 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.