Deserializer

Trait Deserializer 

Source
pub trait Deserializer<T> {
    // Required method
    fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
        &self,
        buffer: &'a [u8],
    ) -> IResult<&'a [u8], T, E>;
}
Expand description

Trait that define the deserialize method that must be implemented for all types have serialize form in Massa.

This trait must be implemented on deserializers that will be defined for each type and can contains constraints. Example:

use std::ops::Bound;
use unsigned_varint::nom as varint_nom;
use nom::{IResult, error::{context, ContextError, ParseError}};
use massa_serialization::Deserializer;
use std::ops::RangeBounds;

pub struct U64VarIntDeserializer {
    range: (Bound<u64>, Bound<u64>)
}

impl U64VarIntDeserializer {
    fn new(min: Bound<u64>, max: Bound<u64>) -> Self {
        Self {
            range: (min, max)
        }
    }
}

impl Deserializer<u64> for U64VarIntDeserializer {
    fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(&self, buffer: &'a [u8]) -> IResult<&'a [u8], u64, E> {
        context(concat!("Failed u64 deserialization"), |input: &'a [u8]| {
            let (rest, value) = varint_nom::u64(input).map_err(|_| nom::Err::Error(ParseError::from_error_kind(input, nom::error::ErrorKind::Fail)))?;
            if !self.range.contains(&value) {
                return Err(nom::Err::Error(ParseError::from_error_kind(input, nom::error::ErrorKind::Fail)));
            }
            Ok((rest, value))
        })(buffer)
    }
}

Required Methods§

Source

fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>( &self, buffer: &'a [u8], ) -> IResult<&'a [u8], T, E>

Deserialize a value T from a buffer of u8.

§Parameters
  • buffer: the buffer that contains the whole serialized data.
§Returns

A nom result with the rest of the serialized data and the decoded value.

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.

Implementors§