Decode

Trait Decode 

Source
pub trait Decode<Format = ()>: Sized {
    // Required method
    fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>;
}
Expand description

Types which can be decoded from storekey format with owned ownership.

Please refer to the documentation of the Encode trait for an explanation of how the data is encoded.

§Implementing decode

Implementing decode is mostly very straight forward.

use std::io::BufRead;

struct MyStruct{
    field_a: u32,
    field_b: String,
}

impl Decode for MyStruct{
    fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>{
        let field_a = Decode::<()>::decode(r)?;
        let field_b = Decode::<()>::decode(r)?;
        Ok(MyStruct{
            field_a,
            field_b
        })
    }
}

enum MyEnum{
    VariantA(u32),
    VariantB(String),
}

impl Decode for MyEnum{
    fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>{
        match r.read_u8()? {
            // One good pattern is to avoid using 0 or 1 as a discriminant as these might need
            // to be escaped
            2 => Ok(MyEnum::VariantA(Decode::<()>::decode(r)?)),
            3 => Ok(MyEnum::VariantB(Decode::<()>::decode(r)?)),
            _ => Err(DecodeError::InvalidFormat)
        }
    }
}

For runtime size typed it is often best to read values in a while loop using the Reader::read_terminal method.

use std::io::BufRead;

struct MyVec(Vec<u8>);

impl Decode for MyVec{
    fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>{
        let mut res = Vec::new();
        while r.read_terminal()? {
            res.push(Decode::<()>::decode(r)?);
        }
        Ok(MyVec(res))
    }
}

Required Methods§

Source

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<'a, F, O> Decode<F> for Cow<'a, O>
where O: ToOwned + ?Sized, O::Owned: Decode<F>,

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F> Decode<F> for bool

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F> Decode<F> for char

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F> Decode<F> for f32

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F> Decode<F> for f64

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F> Decode<F> for i8

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F> Decode<F> for i16

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F> Decode<F> for i32

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F> Decode<F> for i64

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F> Decode<F> for i128

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F> Decode<F> for u8

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F> Decode<F> for u16

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F> Decode<F> for u32

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F> Decode<F> for u64

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F> Decode<F> for u128

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F> Decode<F> for String

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F, D: Decode<F>> Decode<F> for Bound<D>

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F, D: Decode<F>> Decode<F> for Option<D>

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F, D: Decode<F>> Decode<F> for Box<D>

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F, D: Decode<F>> Decode<F> for Vec<D>

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F, K: Decode<F> + Ord, V: Decode<F>> Decode<F> for BTreeMap<K, V>

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F, K: Decode<F> + Hash + Eq, V: Decode<F>, S: BuildHasher + Default> Decode<F> for HashMap<K, V, S>

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F, O: Decode<F>, E: Decode<F>> Decode<F> for Result<O, E>

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<F, T: Decode<F> + Sized, const SIZE: usize> Decode<F> for [T; SIZE]

Source§

fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<Format> Decode<Format> for ()

Source§

fn decode<R: BufRead>(_r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<Format, A: Decode<Format>> Decode<Format> for (A,)

Source§

fn decode<R: BufRead>(_r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<Format, A: Decode<Format>, B: Decode<Format>> Decode<Format> for (A, B)

Source§

fn decode<R: BufRead>(_r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<Format, A: Decode<Format>, B: Decode<Format>, C: Decode<Format>> Decode<Format> for (A, B, C)

Source§

fn decode<R: BufRead>(_r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<Format, A: Decode<Format>, B: Decode<Format>, C: Decode<Format>, D: Decode<Format>> Decode<Format> for (A, B, C, D)

Source§

fn decode<R: BufRead>(_r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<Format, A: Decode<Format>, B: Decode<Format>, C: Decode<Format>, D: Decode<Format>, E: Decode<Format>> Decode<Format> for (A, B, C, D, E)

Source§

fn decode<R: BufRead>(_r: &mut Reader<R>) -> Result<Self, DecodeError>

Source§

impl<Format, A: Decode<Format>, B: Decode<Format>, C: Decode<Format>, D: Decode<Format>, E: Decode<Format>, F: Decode<Format>> Decode<Format> for (A, B, C, D, E, F)

Source§

fn decode<R: BufRead>(_r: &mut Reader<R>) -> Result<Self, DecodeError>

Implementors§