Skip to main content

Encoder

Struct Encoder 

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

Encodes a JSON value into frame payloads, choosing snapshots and deltas automatically.

The track-free core of Producer: it decides what bytes go in a frame and where the group boundaries fall, and leaves writing them to the caller. Reach for it when something else already owns the track, for example a moq_mux::container::Producer that is also managing a timeline and a catalog estimate:

ⓘ
if let Some(frame) = encoder.update(&value)? {
    container.write(moq_mux::container::Frame {
        timestamp,
        decode: None,
        duration: None,
        payload: frame.payload.clone(),
        keyframe: frame.keyframe,
    })?; // an early return here drops `frame`, resetting the encoder
    frame.commit();
}

Frames must reach the wire in the order they were encoded, and a frame with keyframe set must open a new group: the merge patches depend on it. update hands back a Pending rather than a bare Encoded so a frame that never reaches the wire can’t silently desync the encoder: dropping it uncommitted resets, and the next value is encoded as a fresh snapshot. Committing a frame you failed to write is the one way to corrupt the stream.

If the caller cuts a group for its own reasons (a cut, seek, or discontinuity), call reset directly so the next value opens the new group with a snapshot. To open one right away, such as when a joiner wants a fresh group (see Demand::wanted), call snapshot.

Implementations§

Source§

impl<T> Encoder<T>

Source

pub fn new(config: Config) -> Self

Create an encoder with a cold baseline, so the first update is a snapshot.

Source

pub fn value(&self) -> Option<&Value>

The last committed value, or None before the first snapshot commits.

This is the baseline the next delta is diffed against, which is what a caller editing the value in place needs to start from. A frame dropped uncommitted never reached the wire, so its value never lands here.

With deltas disabled the baseline is held as the encoded bytes, so the first call parses them; the result is cached, and callers that never read the value never pay for it.

Source

pub fn reset(&mut self)

Force the next update to emit a full snapshot, even for an unchanged value.

Call this whenever the caller closes the current group behind the encoder’s back (a cut, a seek, a discontinuity). Without it the next value may be encoded as a delta against a baseline that the new group doesn’t carry.

value survives: the snapshot republishes it in full anyway, and it is what a caller editing in place starts from.

Source

pub fn snapshot(&mut self) -> Result<Option<Pending<'_, T>>>

Restate the last committed value as a full snapshot, which must open a new group.

For a caller that opens groups itself: call it when a joiner wants a fresh group, such as when Demand::wanted passes the largest group written. The new group restates the value without the deltas before it, though that joiner still reads the cached group first. Returns None before the first value, when there is nothing to restate. The frame is a Pending like any other.

Source§

impl<T: Serialize> Encoder<T>

Source

pub fn update(&mut self, value: &T) -> Result<Option<Pending<'_, T>>>

Encode a new value, as a snapshot or a delta.

Returns None when the value is unchanged from the last one committed, so nothing needs to be written. Otherwise the frame comes back as a Pending the caller writes and then commits; dropping it uncommitted resynchronizes the encoder.

Auto Trait Implementations§

§

impl<T> !Freeze for Encoder<T>

§

impl<T> !RefUnwindSafe for Encoder<T>

§

impl<T> !Sync for Encoder<T>

§

impl<T> Send for Encoder<T>

§

impl<T> Unpin for Encoder<T>

§

impl<T> UnsafeUnpin for Encoder<T>

§

impl<T> UnwindSafe for Encoder<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> 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, 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