pub enum Codec {
Json,
MsgPack,
}Expand description
Wire codec for the stdin/stdout protocol.
See the module documentation for format details.
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. For hot paths (e.g. rapid event emission), consider pre-allocating and reusing a buffer.
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.
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 JSON arrays of byte values,
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.
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).
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).
Sourcepub fn set_global(codec: Codec)
pub fn set_global(codec: Codec)
Store the negotiated codec in the global slot. Panics if called twice.
Sourcepub fn get_global() -> &'static Codec
pub fn get_global() -> &'static Codec
Get the global wire codec. Returns MsgPack if not yet initialized.
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<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.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more