pub trait BorshSerialize {
// Required method
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()>;
}
Expand description
A data-structure that can be serialized into binary format by NBOR.
use borsh::BorshSerialize;
/// derive is only available if borsh is built with `features = ["derive"]`
#[derive(BorshSerialize)]
struct MyBorshSerializableStruct {
value: String,
}
let x = MyBorshSerializableStruct { value: "hello".to_owned() };
let mut buffer: Vec<u8> = Vec::new();
x.serialize(&mut buffer).unwrap();
let single_serialized_buffer_len = buffer.len();
x.serialize(&mut buffer).unwrap();
assert_eq!(buffer.len(), single_serialized_buffer_len * 2);
let mut buffer: Vec<u8> = vec![0; 1024 + single_serialized_buffer_len];
let mut buffer_slice_enough_for_the_data = &mut buffer[1024..1024 + single_serialized_buffer_len];
x.serialize(&mut buffer_slice_enough_for_the_data).unwrap();
Required Methods§
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.