[−][src]Trait exonum_merkledb::BinaryValue
A type that can be (de)serialized as a value in the blockchain storage.
If you need to implement BinaryValue
for your types, use little-endian encoding
for integer types for compatibility with modern architectures.
Examples
Implementing BinaryValue
for the type:
use std::{borrow::Cow, io::{Read, Write}}; use byteorder::{LittleEndian, ReadBytesExt, ByteOrder}; use failure; use exonum_merkledb::BinaryValue; #[derive(Clone)] struct Data { a: i16, b: u32, } impl BinaryValue for Data { fn to_bytes(&self) -> Vec<u8> { let mut buf = vec![0_u8; 6]; LittleEndian::write_i16(&mut buf[0..2], self.a); LittleEndian::write_u32(&mut buf[2..6], self.b); buf } fn from_bytes(bytes: Cow<[u8]>) -> Result<Self, failure::Error> { let mut buf = bytes.as_ref(); let a = buf.read_i16::<LittleEndian>()?; let b = buf.read_u32::<LittleEndian>()?; Ok(Self { a, b }) } }
Required methods
fn to_bytes(&self) -> Vec<u8>
Serializes the given value to the vector of bytes.
fn from_bytes(bytes: Cow<[u8]>) -> Result<Self, Error>
Deserializes the value from the given bytes array.
Provided methods
fn into_bytes(self) -> Vec<u8>
Consumes and serializes the given value to the vector of bytes. This method is faster with the wrapped values, thus if you wouldn't use value after serialization use it.
Implementations on Foreign Types
impl BinaryValue for u8
[src]
impl BinaryValue for u16
[src]
impl BinaryValue for u32
[src]
impl BinaryValue for u64
[src]
impl BinaryValue for u128
[src]
impl BinaryValue for i8
[src]
impl BinaryValue for i16
[src]
impl BinaryValue for i32
[src]
impl BinaryValue for i64
[src]
impl BinaryValue for i128
[src]
impl BinaryValue for ()
[src]
No-op implementation.