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§
fn serialize(self) -> Bytes ⓘ
fn deserialize(bytes: &Bytes, index: usize) -> Option<Self>
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.