pub struct Producer<T> { /* private fields */ }Expand description
Publishes a JSON value over a track, choosing snapshots and deltas automatically.
An Encoder that owns its track: it writes each encoded frame and rolls a group whenever the
encoder emits a snapshot. When something else already owns the track, use the Encoder
directly.
Cheaply clonable: clones share one underlying track and publishing state, like other MoQ producers.
Implementations§
Source§impl<T> Producer<T>
impl<T> Producer<T>
Sourcepub fn consume(&self) -> Subscriber
pub fn consume(&self) -> Subscriber
Create a subscriber for the underlying track.
Source§impl<T: Serialize> Producer<T>
impl<T: Serialize> Producer<T>
Sourcepub fn new(track: Producer, config: ProducerConfig) -> Self
pub fn new(track: Producer, config: ProducerConfig) -> Self
Create a producer that publishes to the given track.
Sourcepub fn update(&mut self, value: &T) -> Result<()>
pub fn update(&mut self, value: &T) -> Result<()>
Publish a new value, emitting a snapshot or a delta automatically.
Does nothing if the value is unchanged from the previous publish.
Sourcepub fn lock(&mut self) -> Guard<'_, T>where
T: Default + DeserializeOwned,
pub fn lock(&mut self) -> Guard<'_, T>where
T: Default + DeserializeOwned,
Lock the current value for in-place editing, publishing on drop.
The returned Guard derefs to the current value: everything published through this producer
so far, composed, or T::default() if nothing has been. Editing it through DerefMut marks
the guard dirty; when a dirty guard drops it publishes the result, a no-op if unchanged.
After a rejected frame the current value is what the producer last tried to publish, which consumers never received. That is deliberate. The guard exists so independent owners can each edit their own field without clobbering, and dropping a rejected owner’s field would clobber it for whoever edits next, which is the failure this API exists to prevent. The owner whose write failed sees the error and can act on it; the next successful publish is a full snapshot carrying the composed value, so consumers converge on it either way.
This is the counterpart to a callback: hold the guard, mutate, drop. The guard holds the
producer’s lock for its lifetime, so independent owners are serialized: each one starts from
the latest value and their changes compose instead of clobbering. Don’t hold a guard across
an .await, since that keeps the lock held while suspended.
Publishing on drop can fail (a closed track, a value that won’t serialize) and only logs a
warning. Call Guard::commit instead to handle the error.