Trait grammers_tl_types::deserialize::Deserializable
source · pub trait Deserializable {
// Required method
fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
where Self: Sized;
// Provided method
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§
sourcefn deserialize(buf: Buffer<'_, '_>) -> Result<Self>where
Self: Sized,
fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>where Self: Sized,
Deserializes an instance of the type from a given buffer.
Provided Methods§
Implementations on Foreign Types§
source§impl Deserializable for bool
impl Deserializable for bool
source§fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
Deserializes a boolean according to the following definitions:
boolFalse#bc799737 = Bool;
deserializes intofalse
.boolTrue#997275b5 = Bool;
deserializes intotrue
.
Examples
use grammers_tl_types::Deserializable;
assert_eq!(bool::from_bytes(&[0xb5, 0x75, 0x72, 0x99]).unwrap(), true);
assert_eq!(bool::from_bytes(&[0x37, 0x97, 0x79, 0xbc]).unwrap(), false);
source§impl Deserializable for f64
impl Deserializable for f64
source§fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
Deserializes a 64-bit floating point according to the following definition:
double ? = Double;
.
Examples
use std::f64;
use grammers_tl_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);
source§impl Deserializable for i32
impl Deserializable for i32
source§fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
Deserializes a 32-bit signed integer according to the following definition:
int ? = Int;
.
Examples
use grammers_tl_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());
source§impl Deserializable for i64
impl Deserializable for i64
source§fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
Deserializes a 64-bit signed integer according to the following definition:
long ? = Long;
.
Examples
use grammers_tl_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());
source§impl Deserializable for u32
impl Deserializable for u32
source§fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
Deserializes a 32-bit unsigned integer according to the following definition:
int ? = Int;
.
Examples
use grammers_tl_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());
source§impl Deserializable for String
impl Deserializable for String
source§fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
Deserializes a UTF-8 string according to the following definition:
string ? = String;
.
Examples
use grammers_tl_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]
);
source§impl Deserializable for Vec<u8>
impl Deserializable for Vec<u8>
source§fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
Deserializes a vector of bytes as a byte-string according to the following definition:
string ? = String;
.
Examples
use grammers_tl_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]);
source§impl Deserializable for [u8; 16]
impl Deserializable for [u8; 16]
source§fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
Deserializes the 128-bit integer according to the following definition:
int128 4*[ int ] = Int128;
.
Examples
use grammers_tl_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);
source§impl Deserializable for [u8; 32]
impl Deserializable for [u8; 32]
source§fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
Deserializes the 128-bit integer according to the following definition:
int256 8*[ int ] = Int256;
.
Examples
use grammers_tl_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);
source§impl<T: Deserializable> Deserializable for Vec<T>
impl<T: Deserializable> Deserializable for Vec<T>
source§fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
fn deserialize(buf: Buffer<'_, '_>) -> Result<Self>
Deserializes a vector of deserializable items according to the following definition:
vector#1cb5c415 {t:Type} # [ t ] = Vector t;
.
Examples
use grammers_tl_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]);