Trait tl_types::serialize::Serializable
source · [−]pub trait Serializable {
fn serialize(&self, buf: Buffer<'_>);
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
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
.
Implementations on Foreign Types
sourceimpl Serializable for bool
impl Serializable for bool
sourcefn serialize(&self, buf: Buffer<'_>)
fn serialize(&self, buf: Buffer<'_>)
Serializes the boolean according to the following definitions:
false
is serialized asboolFalse#bc799737 = Bool;
.true
is serialized asboolTrue#997275b5 = Bool;
.
Examples
use gramme_types::Serializable;
assert_eq!(true.to_bytes(), [0xb5, 0x75, 0x72, 0x99]);
assert_eq!(false.to_bytes(), [0x37, 0x97, 0x79, 0xbc]);
sourceimpl Serializable for i32
impl Serializable for i32
sourcefn serialize(&self, buf: Buffer<'_>)
fn serialize(&self, buf: Buffer<'_>)
Serializes the 32-bit signed integer according to the following definition:
int ? = Int;
.
Examples
use gramme_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]);
sourceimpl Serializable for u32
impl Serializable for u32
sourcefn serialize(&self, buf: Buffer<'_>)
fn serialize(&self, buf: Buffer<'_>)
Serializes the 32-bit unsigned integer according to the following definition:
int ? = Int;
.
Examples
use gramme_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]);
sourceimpl Serializable for i64
impl Serializable for i64
sourcefn serialize(&self, buf: Buffer<'_>)
fn serialize(&self, buf: Buffer<'_>)
Serializes the 64-bit signed integer according to the following definition:
long ? = Long;
.
Examples
use gramme_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]);
sourceimpl Serializable for [u8; 16]
impl Serializable for [u8; 16]
sourceimpl Serializable for [u8; 32]
impl Serializable for [u8; 32]
sourcefn serialize(&self, buf: Buffer<'_>)
fn serialize(&self, buf: Buffer<'_>)
Serializes the 128-bit integer according to the following definition:
int256 8*[ int ] = Int256;
.
Examples
use gramme_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);
sourceimpl Serializable for f64
impl Serializable for f64
sourcefn serialize(&self, buf: Buffer<'_>)
fn serialize(&self, buf: Buffer<'_>)
Serializes the 64-bit floating point according to the following definition:
double ? = Double;
.
Examples
use std::f64;
use gramme_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]);
sourceimpl<T: Serializable> Serializable for Vec<T>
impl<T: Serializable> Serializable for Vec<T>
sourcefn serialize(&self, buf: Buffer<'_>)
fn serialize(&self, buf: Buffer<'_>)
Serializes a vector of serializable items according to the following definition:
vector#1cb5c415 {t:Type} # [ t ] = Vector t;
.
Examples
use gramme_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]);
sourceimpl Serializable for String
impl Serializable for String
sourcefn serialize(&self, buf: Buffer<'_>)
fn serialize(&self, buf: Buffer<'_>)
Serializes a UTF-8 string according to the following definition:
string ? = String;
.
Examples
use gramme_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]
);
sourceimpl Serializable for Vec<u8>
impl Serializable for Vec<u8>
sourcefn serialize(&self, buf: Buffer<'_>)
fn serialize(&self, buf: Buffer<'_>)
Serializes a vector of bytes as a byte-string according to the following definition:
string ? = String;
.
Examples
use gramme_types::Serializable;
assert_eq!(Vec::<u8>::new().to_bytes(), &[0x00, 0x00, 0x00, 0x00]);
assert_eq!(vec![0x7f_u8].to_bytes(), &[0x01, 0x7f, 0x00, 0x00]);