Skip to main content

ValueEncoder

Trait ValueEncoder 

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

§Type Parameters

  • Input: Borrowed source type accepted by encode. It may be unsized, so implementations can accept slices and string types without an extra reference layer.

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

Source

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.

Source

type Error

Failure reported when one input value cannot be represented.

Implementations choose the error type and determine which encoding failures it represents. Callers observe it as the Err of Self::encode.

Required Methods§

Source

fn encode(&mut self, input: &Input) -> Result<Self::Output, Self::Error>

Encodes input.

§Parameters
  • input: Source value to encode.
§Returns

Encoded output.

§Errors

Returns an error when the codec cannot represent the supplied input.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§