pub trait ValueEncoder<Input: ?Sized> {
type Output;
type Error;
// Required method
fn encode(&mut self, input: &Input) -> Result<Self::Output, Self::Error>;
}Expand description
Encodes a borrowed input value into an owned representation.
This trait is a convenience-layer API. Use crate::Codec for low-level
single-value buffer encoding and crate::Transcoder for batch
conversion over caller-provided buffers.
§Examples
use qubit_codec::ValueEncoder;
struct TextEncoder;
impl ValueEncoder<str> for TextEncoder {
type Output = Vec<u8>;
type Error = core::convert::Infallible;
fn encode(&mut self, input: &str) -> Result<Self::Output, Self::Error> {
Ok(input.as_bytes().to_vec())
}
}
let mut encoder = TextEncoder;
assert_eq!(encoder.encode("hello").unwrap(), b"hello");Required Associated Types§
Sourcetype Output
type Output
Owned representation produced from one input value.
Implementations must return a value that owns its data: Output may not
borrow from Input or from self, so the result outlives the borrow
passed to Self::encode. The representation is free — for example
String or Vec<u8> — and its size is the encoding’s cost.
Sourcetype Error
type Error
Failure reported when one input value cannot be represented.
Implementations choose the error type; anything convertible into it from
a codec or allocation failure is accepted. Callers observe it only as
the Err of Self::encode.
Required Methods§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".