Skip to main content

SyncedStore

Struct SyncedStore 

Source
pub struct SyncedStore<T> { /* private fields */ }
Expand description

A synchronized key-value store with device-owned slices.

Each device owns one slice of type T. Writes update only the local slice and broadcast the change. Remote slices are received and cached automatically.

Created via SyncedStore::new or Node::synced_store.

Implementations§

Source§

impl<T: Serialize + DeserializeOwned + Clone + Send + Sync + 'static> SyncedStore<T>

Source

pub fn new<N: NetworkProvider + 'static>( node: Arc<Node<N>>, store_id: &str, ) -> Arc<Self>

Create a new SyncedStore and start the background sync task.

Uses the default MemoryBackend (no persistence). For durable storage across restarts, use new_with_backend.

The store syncs on namespace "ss:{store_id}". The caller must hold an Arc<Node<N>> because the background task needs to outlive this constructor call.

Source

pub fn new_with_backend<N: NetworkProvider + 'static>( node: Arc<Node<N>>, store_id: &str, backend: Arc<dyn StoreBackend>, ) -> Arc<Self>

Create a new SyncedStore with a custom persistence backend.

On startup, attempts to load the local device’s persisted slice. If found, the in-memory state and version counter are restored so the store picks up where it left off.

Source

pub async fn set(&self, data: T)

Update this device’s data. Increments version and broadcasts.

Source

pub async fn local(&self) -> Option<T>

This device’s current data, or None if set() hasn’t been called.

Source

pub async fn get(&self, device_id: &str) -> Option<Slice<T>>

A specific peer’s slice, or None if we haven’t received their data.

Source

pub async fn all(&self) -> HashMap<String, Slice<T>>

All slices (local + remote).

Source

pub async fn device_ids(&self) -> Vec<String>

Device IDs that have data in this store (local + remote).

Source

pub fn subscribe(&self) -> Receiver<StoreEvent<T>>

Subscribe to store change events.

Source

pub fn store_id(&self) -> &str

The store identifier.

Source

pub fn device_id(&self) -> &str

This device’s ID.

Source

pub fn version(&self) -> u64

Current local version number.

Source

pub async fn stop(&self)

Stop the background sync task.

Trait Implementations§

Source§

impl<T> Drop for SyncedStore<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for SyncedStore<T>

§

impl<T> !RefUnwindSafe for SyncedStore<T>

§

impl<T> Send for SyncedStore<T>
where T: Send + Sync,

§

impl<T> Sync for SyncedStore<T>
where T: Send + Sync,

§

impl<T> Unpin for SyncedStore<T>

§

impl<T> UnsafeUnpin for SyncedStore<T>

§

impl<T> !UnwindSafe for SyncedStore<T>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more