Trait exonum::storage::StorageValue
[−]
[src]
pub trait StorageValue: Sized { fn hash(&self) -> Hash; fn into_bytes(self) -> Vec<u8>; fn from_bytes(value: Cow<[u8]>) -> Self; }
A trait that defines serialization of corresponding types as storage values.
For compatibility with modern architectures the little-endian encoding is used.
Examples
Implementing StorageValue
for the type:
use std::borrow::Cow; use exonum::storage::StorageValue; use exonum::crypto::{self, Hash}; use byteorder::{LittleEndian, ByteOrder}; struct Data { a: i16, b: u32, } impl StorageValue for Data { fn into_bytes(self) -> Vec<u8> { let mut buffer = vec![0; 6]; LittleEndian::write_i16(&mut buffer[0..2], self.a); LittleEndian::write_u32(&mut buffer[2..6], self.b); buffer } fn from_bytes(value: Cow<[u8]>) -> Self { let a = LittleEndian::read_i16(&value[0..2]); let b = LittleEndian::read_u32(&value[2..6]); Data { a, b } } fn hash(&self) -> Hash { let mut buffer = [0; 6]; LittleEndian::write_i16(&mut buffer[0..2], self.a); LittleEndian::write_u32(&mut buffer[2..6], self.b); crypto::hash(&buffer) } }
Required Methods
fn hash(&self) -> Hash
Returns a hash of the value.
This method is actively used to build indices, so the hashing strategy must satisfy the basic requirements of cryptographic hashing: equal values must have the same hash and not equal values must have different hashes.
fn into_bytes(self) -> Vec<u8>
Serialize a value into a vector of bytes.
fn from_bytes(value: Cow<[u8]>) -> Self
Deserialize a value from bytes.
Implementations on Foreign Types
impl StorageValue for ()
[src]
impl StorageValue for u8
[src]
impl StorageValue for u16
[src]
impl StorageValue for u32
[src]
impl StorageValue for u64
[src]
impl StorageValue for i8
[src]
impl StorageValue for i16
[src]
impl StorageValue for i32
[src]
impl StorageValue for i64
[src]
impl StorageValue for Vec<u8>
[src]
impl StorageValue for String
[src]
Implementors
impl StorageValue for Connect
impl StorageValue for Propose
impl StorageValue for Prevote
impl StorageValue for Precommit
impl StorageValue for Status
impl StorageValue for BlockResponse
impl StorageValue for ProposeRequest
impl StorageValue for TransactionsRequest
impl StorageValue for PrevotesRequest
impl StorageValue for PeersRequest
impl StorageValue for BlockRequest
impl StorageValue for Hash
impl StorageValue for PublicKey
impl StorageValue for RawMessage
impl StorageValue for Block
impl StorageValue for TxLocation
impl StorageValue for StoredConfiguration