SyncEngine

Struct SyncEngine 

Source
pub struct SyncEngine<S, D> { /* private fields */ }
Available on crate feature sync only.
Expand description

Sync engine for bidirectional state synchronization

The engine is generic over:

  • S: The state type being synchronized
  • D: The diff type for that state

The engine manages:

  • Version tracking via SyncTracker
  • State snapshots for diff computation
  • Diff generation and application

Implementations§

Source§

impl<S: Clone, D> SyncEngine<S, D>

Source

pub fn new( encode_diff: fn(&D) -> Vec<u8>, decode_diff: fn(&[u8]) -> Result<D, String>, compute_diff: fn(&S, &S) -> D, apply_diff: fn(&mut S, &D) -> Result<(), String>, is_diff_empty: fn(&D) -> bool, ) -> Self

Create a new sync engine with the required callbacks

Source

pub fn init(&mut self, initial_state: S)

Initialize the engine with initial state

Source

pub fn is_initialized(&self) -> bool

Check if the engine is initialized

Source

pub fn state(&self) -> Option<&S>

Get a reference to the current state

Source

pub fn state_mut(&mut self) -> Option<&mut S>

Get a mutable reference to the current state

Note: After modifying, call mark_changed() to bump version

Source

pub fn mark_changed(&mut self) -> u64

Mark that the local state has changed

Call this after modifying the state to bump the version.

Source

pub fn update_state(&mut self, new_state: S) -> u64

Update local state and bump version atomically

Source

pub fn tracker(&self) -> &SyncTracker

Get the tracker for inspection

Source

pub fn has_pending_updates(&self) -> bool

Check if we have updates to send

Source

pub fn needs_ack(&self) -> bool

Check if we need to send an ack

Source

pub fn generate_message(&mut self) -> Result<Option<SyncMessage>, SyncError>

Generate a sync message to send to peer

Returns None if there’s nothing to send

Source

pub fn generate_ack(&self) -> Result<SyncMessage, SyncError>

Generate an ack-only message

Source

pub fn process_message( &mut self, msg: &SyncMessage, ) -> Result<ProcessResult, SyncError>

Process an incoming sync message

Returns the result of processing

Source

pub fn current_version(&self) -> u64

Get current local version

Source

pub fn peer_version(&self) -> u64

Get peer’s version

Source

pub fn is_synchronized(&self) -> bool

Check if synchronized with peer

Source

pub fn reset(&mut self)

Reset the engine

Auto Trait Implementations§

§

impl<S, D> Freeze for SyncEngine<S, D>
where S: Freeze,

§

impl<S, D> RefUnwindSafe for SyncEngine<S, D>
where S: RefUnwindSafe,

§

impl<S, D> Send for SyncEngine<S, D>
where S: Send,

§

impl<S, D> Sync for SyncEngine<S, D>
where S: Sync,

§

impl<S, D> Unpin for SyncEngine<S, D>
where S: Unpin,

§

impl<S, D> UnwindSafe for SyncEngine<S, D>
where S: 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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V