Skip to main content

Codec

Enum Codec 

Source
pub enum Codec {
    Json,
    MsgPack,
}
Expand description

Wire codec for the stdin/stdout protocol.

Json uses newline-delimited JSON. MsgPack uses a 4-byte big-endian length prefix followed by a MessagePack payload.

Variants§

§

Json

Newline-delimited JSON (JSONL).

§

MsgPack

Length-prefixed MessagePack.

Implementations§

Source§

impl Codec

Source

pub fn encode<T: Serialize>(&self, value: &T) -> Result<Vec<u8>, String>

Encode a value to wire bytes ready to write to stdout.

  • JSON: serde_json serialization + trailing \n.
  • MsgPack: 4-byte BE u32 length prefix + rmp_serde named serialization.

Allocates a new Vec per call. In practice, encode is called once per outgoing message (not per render frame), and the messages are small enough that the allocation is negligible relative to the I/O cost. Buffer reuse would add complexity for no measurable gain.

§Errors

Returns an error when serialization fails or when the encoded payload exceeds the 4 GiB msgpack frame limit.

Source

pub fn encode_binary_message( &self, map: Map<String, Value>, binary_field: Option<(&str, &[u8])>, ) -> Result<Vec<u8>, String>

Encode a JSON map with an optional binary field to wire bytes.

For MsgPack: binary fields are encoded as native msgpack binary (rmpv::Value::Binary), avoiding the ~33% size overhead of base64. The map is built via rmpv::Value::Map to preserve the binary type.

For JSON: binary fields are base64-encoded as strings.

Use this instead of encode when the message contains raw byte data (e.g. pixel buffers) that should use native binary encoding over msgpack.

§Errors

Returns an error when JSON or msgpack serialization fails, or when the encoded msgpack payload exceeds the 4 GiB frame limit.

Source

pub fn decode<T: DeserializeOwned>(&self, bytes: &[u8]) -> Result<T, String>

Decode a raw payload (framing already stripped) into a typed value.

For JSON, bytes is the UTF-8 JSON text (without the trailing newline). For MsgPack, bytes is the raw msgpack payload (without the length prefix).

MsgPack decoding routes through rmpv::Value as an intermediate. This preserves binary data (msgpack’s bin type) as base64 strings, which the deserialize_binary_field custom deserializer in protocol.rs can reconstruct into Vec<u8>. The serde_json::Value intermediate is still needed for tag dispatch (#[serde(tag = "type")]) which rmp-serde doesn’t handle reliably for externally-produced msgpack.

§Errors

Returns an error when the payload fails the msgpack depth check, cannot be decoded from the selected wire format, or cannot be deserialized into the requested target type.

Source

pub fn read_message<R: BufRead>( &self, reader: &mut R, ) -> Result<Option<Vec<u8>>>

Read one framed message from a buffered reader, returning the raw payload.

  • JSON: reads until \n, returns the line bytes (without the newline).
  • MsgPack: reads a 4-byte BE u32 length, then reads that many bytes.

Returns Ok(None) on EOF (clean shutdown).

§Errors

Returns an I/O error when the reader fails, when a JSON line or msgpack frame exceeds MAX_MESSAGE_SIZE, or when msgpack framing is truncated or otherwise malformed.

Source

pub fn detect_from_first_byte(byte: u8) -> Codec

Detect codec from the first byte of input.

{ (0x7B) indicates JSON. Anything else indicates MsgPack (the first byte of a 4-byte length prefix).

Trait Implementations§

Source§

impl Clone for Codec

Source§

fn clone(&self) -> Codec

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

impl Debug for Codec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Codec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Codec

Source§

fn eq(&self, other: &Codec) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Codec

Source§

impl Eq for Codec

Source§

impl StructuralPartialEq for Codec

Auto Trait Implementations§

§

impl Freeze for Codec

§

impl RefUnwindSafe for Codec

§

impl Send for Codec

§

impl Sync for Codec

§

impl Unpin for Codec

§

impl UnsafeUnpin for Codec

§

impl UnwindSafe for Codec

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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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<State, Message> IntoBoot<State, Message> for State

Source§

fn into_boot(self) -> (State, Task<Message>)

Turns some type into the initial state of some Application.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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> ToSmolStr for T
where T: Display + ?Sized,

Source§

impl<T> ToSmolStr for T
where T: Display + ?Sized,

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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
Source§

impl<T> MaybeClone for T

Source§

impl<T> MaybeDebug for T

Source§

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

Source§

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