Trait grammers_tl_types::serialize::Serializable
source · pub trait Serializable {
// Required method
fn serialize(&self, buf: &mut impl Extend<u8>);
// Provided method
fn to_bytes(&self) -> Vec<u8> { ... }
}
Expand description
This trait allows for concrete instances to be serialized into binary data as specified by the Binary Data Serialization.
Required Methods§
Provided Methods§
sourcefn to_bytes(&self) -> Vec<u8>
fn to_bytes(&self) -> Vec<u8>
Convenience function to serialize the object into a new buffer
and return its bytes. It is more efficient to reuse a existing
buffer with Serializable::serialize
.
Object Safety§
Implementations on Foreign Types§
source§impl Serializable for &[u8]
impl Serializable for &[u8]
source§impl Serializable for bool
impl Serializable for bool
source§fn serialize(&self, buf: &mut impl Extend<u8>)
fn serialize(&self, buf: &mut impl Extend<u8>)
Serializes the boolean according to the following definitions:
false
is serialized asboolFalse#bc799737 = Bool;
.true
is serialized asboolTrue#997275b5 = Bool;
.
§Examples
use grammers_tl_types::Serializable;
assert_eq!(true.to_bytes(), [0xb5, 0x75, 0x72, 0x99]);
assert_eq!(false.to_bytes(), [0x37, 0x97, 0x79, 0xbc]);
source§impl Serializable for f64
impl Serializable for f64
source§fn serialize(&self, buf: &mut impl Extend<u8>)
fn serialize(&self, buf: &mut impl Extend<u8>)
Serializes the 64-bit floating point according to the following definition:
double ? = Double;
.
§Examples
use std::f64;
use grammers_tl_types::Serializable;
assert_eq!(0f64.to_bytes(), [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]);
assert_eq!(1.5f64.to_bytes(), [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf8, 0x3f]);
assert_eq!((-1.5f64).to_bytes(), [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf8, 0xbf]);
assert_eq!(f64::INFINITY.to_bytes(), [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf0, 0x7f]);
assert_eq!(f64::NEG_INFINITY.to_bytes(), [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf0, 0xff]);
source§impl Serializable for i32
impl Serializable for i32
source§fn serialize(&self, buf: &mut impl Extend<u8>)
fn serialize(&self, buf: &mut impl Extend<u8>)
Serializes the 32-bit signed integer according to the following definition:
int ? = Int;
.
§Examples
use grammers_tl_types::Serializable;
assert_eq!(0i32.to_bytes(), [0x00, 0x00, 0x00, 0x00]);
assert_eq!(1i32.to_bytes(), [0x01, 0x00, 0x00, 0x00]);
assert_eq!((-1i32).to_bytes(), [0xff, 0xff, 0xff, 0xff]);
assert_eq!(i32::max_value().to_bytes(), [0xff, 0xff, 0xff, 0x7f]);
assert_eq!(i32::min_value().to_bytes(), [0x00, 0x00, 0x00, 0x80]);
source§impl Serializable for i64
impl Serializable for i64
source§fn serialize(&self, buf: &mut impl Extend<u8>)
fn serialize(&self, buf: &mut impl Extend<u8>)
Serializes the 64-bit signed integer according to the following definition:
long ? = Long;
.
§Examples
use grammers_tl_types::Serializable;
assert_eq!(0i64.to_bytes(), [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]);
assert_eq!(1i64.to_bytes(), [0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]);
assert_eq!((-1i64).to_bytes(), [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
assert_eq!(i64::max_value().to_bytes(), [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]);
assert_eq!(i64::min_value().to_bytes(), [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80]);
source§impl Serializable for u32
impl Serializable for u32
source§fn serialize(&self, buf: &mut impl Extend<u8>)
fn serialize(&self, buf: &mut impl Extend<u8>)
Serializes the 32-bit unsigned integer according to the following definition:
int ? = Int;
.
§Examples
use grammers_tl_types::Serializable;
assert_eq!(0u32.to_bytes(), [0x00, 0x00, 0x00, 0x00]);
assert_eq!(1u32.to_bytes(), [0x01, 0x00, 0x00, 0x00]);
assert_eq!(u32::max_value().to_bytes(), [0xff, 0xff, 0xff, 0xff]);
assert_eq!(u32::min_value().to_bytes(), [0x00, 0x00, 0x00, 0x00]);
source§impl Serializable for String
impl Serializable for String
source§fn serialize(&self, buf: &mut impl Extend<u8>)
fn serialize(&self, buf: &mut impl Extend<u8>)
Serializes a UTF-8 string according to the following definition:
string ? = String;
.
§Examples
use grammers_tl_types::Serializable;
fn test_string(string: &str, prefix: &[u8], suffix: &[u8]) {
let bytes = string.to_owned().to_bytes();
let expected = {
let mut tmp = prefix.to_vec();
tmp.extend(string.as_bytes());
tmp.extend(suffix);
tmp
};
assert_eq!(bytes, 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 Serializable for Vec<u8>
impl Serializable for Vec<u8>
source§fn serialize(&self, buf: &mut impl Extend<u8>)
fn serialize(&self, buf: &mut impl Extend<u8>)
Serializes a vector of bytes as a byte-string according to the following definition:
string ? = String;
.
§Examples
use grammers_tl_types::Serializable;
assert_eq!(Vec::<u8>::new().to_bytes(), &[0x00, 0x00, 0x00, 0x00]);
assert_eq!(vec![0x7f_u8].to_bytes(), &[0x01, 0x7f, 0x00, 0x00]);
source§impl Serializable for [u8; 16]
impl Serializable for [u8; 16]
source§fn serialize(&self, buf: &mut impl Extend<u8>)
fn serialize(&self, buf: &mut impl Extend<u8>)
Serializes the 128-bit integer according to the following definition:
int128 4*[ int ] = Int128;
.
§Examples
use grammers_tl_types::Serializable;
let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
assert_eq!(data.to_bytes(), data);
source§impl Serializable for [u8; 32]
impl Serializable for [u8; 32]
source§fn serialize(&self, buf: &mut impl Extend<u8>)
fn serialize(&self, buf: &mut impl Extend<u8>)
Serializes the 128-bit integer according to the following definition:
int256 8*[ int ] = Int256;
.
§Examples
use grammers_tl_types::Serializable;
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!(data.to_bytes(), data);
source§impl<T: Serializable> Serializable for Vec<T>
impl<T: Serializable> Serializable for Vec<T>
source§fn serialize(&self, buf: &mut impl Extend<u8>)
fn serialize(&self, buf: &mut impl Extend<u8>)
Serializes a vector of serializable items according to the following definition:
vector#1cb5c415 {t:Type} # [ t ] = Vector t;
.
§Examples
use grammers_tl_types::Serializable;
assert_eq!(Vec::<i32>::new().to_bytes(), [0x15, 0xc4, 0xb5, 0x1c, 0x0, 0x0, 0x0, 0x0]);
assert_eq!(vec![0x7f_i32].to_bytes(),
[0x15, 0xc4, 0xb5, 0x1c, 0x1, 0x0, 0x0, 0x0, 0x7f, 0x0, 0x0, 0x0]);