Struct defmt::Encoder

source ·
pub struct Encoder { /* private fields */ }
Expand description

Encode raw defmt frames for sending over the wire.

defmt emits “log frames”, which are sequences of bytes. The raw log frame data is then encoded prior to sending over the wire.

Encoder will encode the frames according to the currently selected encoding-* Cargo feature. See Cargo.toml for the supported encodings and their tradeoffs.

Encodings may perform two functions:

  • Framing: Adds extra data to allow the encoder to know when each frame starts and ends in the stream. Unframed log frames already contain enough information for the decoder to know when they end, so framing is optional. However, without framing the decoder must receive all bytes intact or it may “lose sync”. With framing, it can recover from missing/corrupted data, and can start decoding from the “middle” of an already-running stream.
  • Compression: The frame data has rather low entropy (for example, it contains many zero bytes due to encoding all integers in fixed with, and will likely contain many repetitions). Compression can decrease the on-the-wire required bandwidth.

defmt provides the Encoder separately instead of feeding already-encoded bytes to the Logger because Logger implementations may decide to allow concurrent logging from multiple “contexts” such as threads or interrupt priority levels. In this case, the Logger implementation needs to create one Encoder for each such context.

Implementations§

source§

impl Encoder

source

pub const fn new() -> Self

Create a new Encoder.

source

pub fn start_frame(&mut self, write: impl FnMut(&[u8]))

Start encoding a log frame.

Logger impls will typically call this from acquire().

You may only call start_frame when no frame is currently being encoded. Failure to do so may result in corrupted data on the wire.

The write closure will be called with the encoded data that must be sent on the wire. It may be called zero, one, or multiple times.

source

pub fn end_frame(&mut self, write: impl FnMut(&[u8]))

Finish encoding a log frame.

Logger impls will typically call this from release().

You may only call end_frame when a frame is currently being encoded. Failure to do so may result in corrupted data on the wire.

The write closure will be called with the encoded data that must be sent on the wire. It may be called zero, one, or multiple times.

source

pub fn write(&mut self, data: &[u8], write: impl FnMut(&[u8]))

Write part of data for a log frame.

Logger impls will typically call this from write().

You may only call write when a frame is currently being encoded. Failure to do so may result in corrupted data on the wire.

The write closure will be called with the encoded data that must be sent on the wire. It may be called zero, one, or multiple times.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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, U> Into<U> for Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.