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 synchronizedD: 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>
impl<S: Clone, D> SyncEngine<S, D>
Sourcepub 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
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
Sourcepub fn is_initialized(&self) -> bool
pub fn is_initialized(&self) -> bool
Check if the engine is initialized
Sourcepub fn state_mut(&mut self) -> Option<&mut S>
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
Sourcepub fn mark_changed(&mut self) -> u64
pub fn mark_changed(&mut self) -> u64
Mark that the local state has changed
Call this after modifying the state to bump the version.
Sourcepub fn update_state(&mut self, new_state: S) -> u64
pub fn update_state(&mut self, new_state: S) -> u64
Update local state and bump version atomically
Sourcepub fn tracker(&self) -> &SyncTracker
pub fn tracker(&self) -> &SyncTracker
Get the tracker for inspection
Sourcepub fn has_pending_updates(&self) -> bool
pub fn has_pending_updates(&self) -> bool
Check if we have updates to send
Sourcepub fn generate_message(&mut self) -> Result<Option<SyncMessage>, SyncError>
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
Sourcepub fn generate_ack(&self) -> Result<SyncMessage, SyncError>
pub fn generate_ack(&self) -> Result<SyncMessage, SyncError>
Generate an ack-only message
Sourcepub fn process_message(
&mut self,
msg: &SyncMessage,
) -> Result<ProcessResult, SyncError>
pub fn process_message( &mut self, msg: &SyncMessage, ) -> Result<ProcessResult, SyncError>
Process an incoming sync message
Returns the result of processing
Sourcepub fn current_version(&self) -> u64
pub fn current_version(&self) -> u64
Get current local version
Sourcepub fn peer_version(&self) -> u64
pub fn peer_version(&self) -> u64
Get peer’s version
Sourcepub fn is_synchronized(&self) -> bool
pub fn is_synchronized(&self) -> bool
Check if synchronized with peer
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> 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