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,
        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: both the merge patches and the group-scoped DEFLATE window 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.

Implementations§

Source§

impl<T> Encoder<T>

Source

pub fn new(config: ProducerConfig) -> Self

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

Source

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

The last encoded value, or None before the first snapshot.

This is the baseline the next delta is diffed against, which is what a caller editing the value in place needs to start from.

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 DEFLATE window and 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§

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 encoded, 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> Send for Encoder<T>

§

impl<T> Sync 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> MaybeSend for T
where T: Send,

Source§

impl<T> MaybeSend for T
where T: Send,

Source§

impl<T> MaybeSync for T
where T: Sync,

Source§

impl<T> MaybeSync for T
where T: Sync,

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