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§
Implementations§
Source§impl Codec
impl Codec
Sourcepub fn encode<T: Serialize>(&self, value: &T) -> Result<Vec<u8>, String>
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_jsonserialization + trailing\n. - MsgPack: 4-byte BE u32 length prefix +
rmp_serdenamed 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.
Sourcepub fn encode_binary_message(
&self,
map: Map<String, Value>,
binary_field: Option<(&str, &[u8])>,
) -> Result<Vec<u8>, String>
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.
Sourcepub fn decode<T: DeserializeOwned>(&self, bytes: &[u8]) -> Result<T, String>
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.
Sourcepub fn read_message<R: BufRead>(
&self,
reader: &mut R,
) -> Result<Option<Vec<u8>>>
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.
Sourcepub fn detect_from_first_byte(byte: u8) -> Codec
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§
impl Copy for Codec
impl Eq for Codec
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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
impl<T> DowncastSync for T
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<State, Message> IntoBoot<State, Message> for State
impl<State, Message> IntoBoot<State, Message> for State
Source§fn into_boot(self) -> (State, Task<Message>)
fn into_boot(self) -> (State, Task<Message>)
Application.