Skip to main content

ValueDecoder

Trait ValueDecoder 

Source
pub trait ValueDecoder<Input: ?Sized> {
    type Output;
    type Error;

    // Required method
    fn decode(&mut self, input: &Input) -> Result<Self::Output, Self::Error>;
}
Expand description

Decodes a borrowed input value into an owned representation.

This trait is a convenience-layer API. Use crate::Codec for low-level single-value buffer decoding and crate::Transcoder for batch conversion over caller-provided buffers.

§Type Parameters

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

§Examples

use core::convert::Infallible;

use qubit_codec::ValueDecoder;

struct Identity;
impl ValueDecoder<str> for Identity {
    type Output = String;
    type Error = Infallible;

    fn decode(&mut self, input: &str) -> Result<Self::Output, Self::Error> {
        Ok(input.to_owned())
    }
}

let mut decoder = Identity;
assert_eq!(decoder.decode("hello").unwrap(), "hello");

Required Associated Types§

Source

type Output

Owned value produced from one successfully decoded Input.

The output type is not tied to the temporary borrow of input passed to decode. It may still borrow data carried by Input, such as an &'a str input whose output also has lifetime 'a.

Source

type Error

Error reported when Input cannot be decoded.

Implementations report malformed or unsupported input here; callers cannot observe partial results because a failed call yields no value.

Required Methods§

Source

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

Decodes input.

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

Returns the owned value decoded from input.

§Errors

Returns an error when the input is malformed or unsupported by the codec.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§