Skip to main content

Producer

Struct Producer 

Source
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.

A group runs until a joiner wants a fresh one, which poll_refresh answers, or until it reaches the encoder’s caps. Set the track’s refresh ratio when creating it, or joins never ask.

Set compression on that track’s info too: JSON is small and repetitive, and moq-net compresses each delta against the snapshot earlier in its group.

Cheaply clonable: clones share one underlying track and publishing state, like other MoQ producers.

Implementations§

Source§

impl<T> Producer<T>

Source

pub fn consume(&self) -> Subscriber

Create a subscriber for the underlying track.

Source

pub fn demand(&self) -> Demand

A watch-only handle to the underlying track’s subscriber demand.

Weak, so holding it neither keeps the track open nor contends with publishing.

Source§

impl<T: Serialize> Producer<T>

Source

pub fn new(track: Producer, config: Config) -> Self

Create a producer that publishes to the given track.

Source

pub fn poll_refresh(&self, waiter: &Waiter) -> Poll<Result<()>>

Wait for a joiner to want a fresh group, then restate the current value as a snapshot in one.

A join wants one once the open group’s deltas outweigh the track’s refresh ratio times its snapshot (see Demand::wanted). It is a soft trigger: that joiner still reads the cached group, then the new one. The snapshot carries the newest timestamp written, where its group starts; while that is the newest group’s own start, the refresh waits for a later update, since two groups never share a start. Nothing happens before the first update, or on a track without a ratio.

The producer has no runtime of its own, so the owner drives this: loop on it, or select on it beside the updates. Errors once the track is closed.

Source

pub async fn refresh(&self) -> Result<()>

Wait for a joiner to want a fresh group, then restate the current value as a snapshot in one.

A join wants one once the open group’s deltas outweigh the track’s refresh ratio times its snapshot (see Demand::wanted). It is a soft trigger: that joiner still reads the cached group, then the new one. The snapshot carries the newest timestamp written, where its group starts; while that is the newest group’s own start, the refresh waits for a later update, since two groups never share a start. Nothing happens before the first update, or on a track without a ratio.

The producer has no runtime of its own, so the owner drives this: loop on it, or select on it beside the updates. Errors once the track is closed.

Awaits poll_refresh.

Source

pub fn update(&mut self, value: &T, at: Timestamp) -> Result<Option<usize>>

Publish a new value at at (see Timed::at), emitting a snapshot or a delta automatically.

Returns the encoded size of the frame written, or None if the value is unchanged from the previous publish and nothing was written.

Source

pub fn mutate(&mut self, f: impl FnOnce(&mut T), at: Timestamp) -> Result<()>

Edit the current value in place and publish it at at (see Timed::at).

The closure receives the current value: everything published through this producer so far, composed, or T::default() if nothing has been. Edit it in place; on return the result is published, a no-op if unchanged.

producer.mutate(|catalog| {
    catalog["scte35"] = serde_json::json!({ "id": 1 });
}, moq_net::Timestamp::from_millis(100).unwrap())

Independent owners can share one producer and each edit only their own keys: every call starts from the latest value, so their sections compose instead of clobbering one another. This is modify opened and committed for you; take the guard to hold the lock across several edits, and update to replace the whole value.

Source

pub fn modify(&mut self, at: Timestamp) -> Result<Guard<'_, T>>

Edit the current value, publishing on drop at at (see Timed::at).

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.

A rejected edit never reached the wire, so the current value stays the last one that did. The owner whose write failed sees the error and can retry; carrying the rejected edit forward would instead fail every later owner’s publish the same way, or publish a value its owner was told failed.

Reach for this over mutate when several edits belong to one publish. 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.

Fails if the track is closed. That is the one publication failure that happens in normal operation, and the guard holds the lock that finish needs, so nothing is left to check after the guard drops. Anything else that stops the drop from publishing (a value that won’t serialize, or one too large for a frame) aborts the track with that error: consumers see it instead of a stale value, and the next modify returns it here. Call Guard::commit to get the error back immediately instead. A panic while the guard is held discards the in-flight edit rather than publishing a torn value.

Source

pub fn cut(&mut self) -> Result<()>

Close the open group, so the deltas already written stop being provisional.

No replacement group opens until the next update or poll_refresh, which emits a full snapshot as its first frame even when the value is unchanged. A consumer joining at that group therefore reads the whole value without the deltas that preceded it.

Idempotent: cutting when no group is open does nothing, so a caller can cut on its own schedule without tracking what has been published since the last one. Inert when deltas are disabled, where every frame already gets its own group.

Source

pub fn finish(self) -> Result<Finished<T>>

Finish the track, closing any open group, and return the Finished value.

Clones sharing the track see it ended: their next modify or update fails. A finish that fails aborts the track with the cause, which those clones then report.

Source

pub fn abort(self, err: Error) -> Result<()>

Abort the track with the given error, which consumers see in place of any further value.

Consumes the handle, since nothing can be published afterwards. Clones sharing the track see it closed: their next modify or update fails.

Trait Implementations§

Source§

impl<T> Clone for Producer<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Producer<T>

§

impl<T> RefUnwindSafe for Producer<T>

§

impl<T> Send for Producer<T>

§

impl<T> Sync for Producer<T>

§

impl<T> Unpin for Producer<T>

§

impl<T> UnsafeUnpin for Producer<T>

§

impl<T> UnwindSafe for Producer<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<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