Skip to main content

StatePublisher

Struct StatePublisher 

Source
pub struct StatePublisher<T: DeltaState> { /* private fields */ }
Expand description

Publishes application-state updates as DeltaStream packets or bytes.

A publisher owns the sending side of one state stream. It observes each new application state and emits either a snapshot or a delta packet according to the configured adaptive policy. The usual flow is:

application state -> Publisher -> snapshot or delta packet -> serialized bytes.

Use Publisher::encode for the simple byte-oriented API, or Publisher::update when an integration needs direct access to packet metadata before serialization.

§Example

use delta_stream::{DeltaState, Publisher};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, DeltaState)]
struct State {
    progress: u8,
}

let mut publisher = Publisher::<State>::new();
let bytes = publisher.encode(&State { progress: 10 })?;
assert!(!bytes.is_empty());

Implementations§

Source§

impl<T: DeltaState> StatePublisher<T>

Source

pub fn new() -> Self

Creates a publisher with the default adaptive policy.

Source

pub fn builder() -> PublisherBuilder<T>

Starts building a publisher with custom settings.

Source

pub fn with_policy(policy: AdaptivePolicy) -> Self

Creates a publisher with an explicit adaptive encoding policy.

Source

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

Encodes a new authoritative state into transport-independent bytes.

This is the high-level publishing path. It creates either a snapshot or delta with the same logic as Publisher::update, then serializes that packet with Packet::to_bytes. The returned bytes can be carried by any byte-capable transport, including TCP, WebSocket, PubNub, MQTT, NATS, UDP with reliability handled externally, files, IPC, or an in-memory channel.

Source

pub fn update(&mut self, state: &T) -> Result<Packet, DeltaError>

Encodes a new authoritative state as a packet.

The first update is a snapshot. Later updates may be deltas or snapshots, and raw or zstd-compressed, depending on the active AdaptivePolicy. This method preserves the stream sequence, schema hash, base hash, and compression behavior used by the lower-level packet API.

Source

pub fn force_snapshot(&mut self, state: &T) -> Result<Packet, DeltaError>

Forces a new snapshot and advances the stream sequence.

Source

pub fn recovery_snapshot(&self, state: &T) -> Result<Packet, DeltaError>

Builds a recovery snapshot at the current stream sequence.

This does not advance the publisher sequence, so recovering one client does not create a sequence gap for healthy clients.

Source

pub fn sequence(&self) -> u64

Returns the current publisher sequence.

Source

pub fn last_decision(&self) -> Option<EncodeDecision>

Returns the most recent adaptive encoding decision, if one exists.

Trait Implementations§

Source§

impl<T: DeltaState> Default for StatePublisher<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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