pub trait Deserializable {
    fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
    where
        Self: Sized
; fn from_bytes(buf: &[u8]) -> Result<Self>
    where
        Self: Sized
, { ... } }
Expand description

This trait allows for data serialized according to the Binary Data Serialization to be deserialized into concrete instances.

Required methods

Deserializes an instance of the type from a given buffer.

Provided methods

Convenience function to deserialize an instance from a given buffer.

Examples
use gramme_types::Deserializable as _;

assert_eq!(bool::from_bytes(&[0x37, 0x97, 0x79, 0xbc]).unwrap(), false);

Implementations on Foreign Types

Deserializes a boolean according to the following definitions:

  • boolFalse#bc799737 = Bool; deserializes into false.
  • boolTrue#997275b5 = Bool; deserializes into true.
Examples
use gramme_types::Deserializable;

assert_eq!(bool::from_bytes(&[0xb5, 0x75, 0x72, 0x99]).unwrap(), true);
assert_eq!(bool::from_bytes(&[0x37, 0x97, 0x79, 0xbc]).unwrap(), false);

Deserializes a 32-bit signed integer according to the following definition:

  • int ? = Int;.
Examples
use gramme_types::Deserializable;

assert_eq!(i32::from_bytes(&[0x00, 0x00, 0x00, 0x00]).unwrap(), 0i32);
assert_eq!(i32::from_bytes(&[0x01, 0x00, 0x00, 0x00]).unwrap(), 1i32);
assert_eq!(i32::from_bytes(&[0xff, 0xff, 0xff, 0xff]).unwrap(), -1i32);
assert_eq!(i32::from_bytes(&[0xff, 0xff, 0xff, 0x7f]).unwrap(), i32::max_value());
assert_eq!(i32::from_bytes(&[0x00, 0x00, 0x00, 0x80]).unwrap(), i32::min_value());

Deserializes a 32-bit unsigned integer according to the following definition:

  • int ? = Int;.
Examples
use gramme_types::Deserializable;

assert_eq!(u32::from_bytes(&[0x00, 0x00, 0x00, 0x00]).unwrap(), 0u32);
assert_eq!(u32::from_bytes(&[0x01, 0x00, 0x00, 0x00]).unwrap(), 1u32);
assert_eq!(u32::from_bytes(&[0xff, 0xff, 0xff, 0xff]).unwrap(), u32::max_value());
assert_eq!(u32::from_bytes(&[0x00, 0x00, 0x00, 0x00]).unwrap(), u32::min_value());

Deserializes a 64-bit signed integer according to the following definition:

  • long ? = Long;.
Examples
use gramme_types::Deserializable;

assert_eq!(i64::from_bytes(&[0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]).unwrap(), 0i64);
assert_eq!(i64::from_bytes(&[0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]).unwrap(), 1i64);
assert_eq!(i64::from_bytes(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]).unwrap(), (-1i64));
assert_eq!(i64::from_bytes(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]).unwrap(), i64::max_value());
assert_eq!(i64::from_bytes(&[0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80]).unwrap(), i64::min_value());

Deserializes the 128-bit integer according to the following definition:

  • int128 4*[ int ] = Int128;.
Examples
use gramme_types::Deserializable;

let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];

assert_eq!(<[u8; 16]>::from_bytes(&data).unwrap(), data);

Deserializes the 128-bit integer according to the following definition:

  • int256 8*[ int ] = Int256;.
Examples
use gramme_types::Deserializable;

let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
            18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32];

assert_eq!(<[u8; 32]>::from_bytes(&data).unwrap(), data);

Deserializes a 64-bit floating point according to the following definition:

  • double ? = Double;.
Examples
use std::f64;
use gramme_types::Deserializable;

assert_eq!(f64::from_bytes(&[0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]).unwrap(), 0f64);
assert_eq!(f64::from_bytes(&[0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf8, 0x3f]).unwrap(), 1.5f64);
assert_eq!(f64::from_bytes(&[0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf8, 0xbf]).unwrap(), -1.5f64);
assert_eq!(f64::from_bytes(&[0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf0, 0x7f]).unwrap(), f64::INFINITY);
assert_eq!(f64::from_bytes(&[0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf0, 0xff]).unwrap(), f64::NEG_INFINITY);

Deserializes a vector of deserializable items according to the following definition:

  • vector#1cb5c415 {t:Type} # [ t ] = Vector t;.
Examples
use gramme_types::Deserializable;

assert_eq!(Vec::<i32>::from_bytes(&[0x15, 0xc4, 0xb5, 0x1c, 0x0, 0x0, 0x0, 0x0]).unwrap(), Vec::new());
assert_eq!(Vec::<i32>::from_bytes(&[0x15, 0xc4, 0xb5, 0x1c, 0x1, 0x0, 0x0, 0x0, 0x7f, 0x0, 0x0, 0x0]).unwrap(),
           vec![0x7f_i32]);

Deserializes a UTF-8 string according to the following definition:

  • string ? = String;.
Examples
use gramme_types::Deserializable;

fn test_string(string: &str, prefix: &[u8], suffix: &[u8]) {
   let bytes = {
       let mut tmp = prefix.to_vec();
       tmp.extend(string.as_bytes());
       tmp.extend(suffix);
       tmp
   };
   let expected = string.to_owned();

   assert_eq!(String::from_bytes(&bytes).unwrap(), expected);
}

test_string("", &[0x00], &[0x00, 0x00, 0x00]);
test_string("Hi", &[0x02], &[0x0]);
test_string("Hi!", &[0x03], &[]);
test_string("Hello", &[0x05], &[0x0, 0x0]);
test_string("Hello, world!", &[0xd], &[0x0, 0x0]);
test_string(
    "This is a very long string, and it has to be longer than 253 \
     characters, which are quite a few but we can make it! Although, \
     it is quite challenging. The quick brown fox jumps over the lazy \
     fox. There is still some more text we need to type. Oh, this \
     sentence made it past!",
     &[0xfe, 0x11, 0x01, 0x00],
     &[0x00, 0x00, 0x00]
);

Deserializes a vector of bytes as a byte-string according to the following definition:

  • string ? = String;.
Examples
use gramme_types::{Deserializable};

assert_eq!(Vec::<u8>::from_bytes(&[0x00, 0x00, 0x00, 0x00]).unwrap(), Vec::new());
assert_eq!(Vec::<u8>::from_bytes(&[0x01, 0x7f, 0x00, 0x00]).unwrap(), vec![0x7f_u8]);

Implementors