Trait Serialize

Source
pub trait Serialize: Sized {
    // Required methods
    fn serialize(self) -> Bytes ;
    fn deserialize(bytes: &Bytes, index: usize) -> Option<Self>;
    fn size(&self) -> usize;
}
Expand description

This is the main part of this crate implement this trait for the things you want to serialize and deserialize

this is meant to be used for saving things in files or transmitting things through buffers

§Warning

the normal String has maximum length of 255 if you want longer strings use the U16String wich allows for strings with a length 65535

§Example

use crate::serialize::Serialize;
// string implements serialize
let string = "Hello World".to_string();
// serialize the string
let bytes = string.clone().serialize();
// deserialize from the beginning of the bytes
let found_string = String::deserialize(&bytes, 0).unwrap();
// the result will be the same
assert_eq!(string, found_string);

Required Methods§

Source

fn serialize(self) -> Bytes

Source

fn deserialize(bytes: &Bytes, index: usize) -> Option<Self>

Source

fn size(&self) -> usize

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.

Implementations on Foreign Types§

Source§

impl Serialize for char

Source§

fn serialize(self) -> Bytes

Source§

fn deserialize(bytes: &Bytes, index: usize) -> Option<Self>

Source§

fn size(&self) -> usize

Source§

impl Serialize for u8

Source§

fn serialize(self) -> Bytes

Source§

fn deserialize(bytes: &Bytes, index: usize) -> Option<Self>

Source§

fn size(&self) -> usize

Source§

impl Serialize for u16

Source§

fn serialize(self) -> Bytes

Source§

fn deserialize(bytes: &Bytes, index: usize) -> Option<Self>

Source§

fn size(&self) -> usize

Source§

impl Serialize for u32

Source§

fn serialize(self) -> Bytes

Source§

fn deserialize(bytes: &Bytes, index: usize) -> Option<Self>

Source§

fn size(&self) -> usize

Source§

impl Serialize for u64

Source§

fn serialize(self) -> Bytes

Source§

fn deserialize(bytes: &Bytes, index: usize) -> Option<Self>

Source§

fn size(&self) -> usize

Source§

impl Serialize for String

Source§

fn serialize(self) -> Bytes

Source§

fn deserialize(bytes: &Bytes, index: usize) -> Option<Self>

Source§

fn size(&self) -> usize

Source§

impl<T: Serialize> Serialize for Vec<T>

Source§

fn serialize(self) -> Bytes

Source§

fn deserialize(bytes: &Bytes, index: usize) -> Option<Self>

Source§

fn size(&self) -> usize

Implementors§