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

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.

Serialize a value into a vector of bytes.

Deserialize a value from bytes.

Implementations on Foreign Types

impl StorageValue for ()
[src]

[src]

[src]

[src]

impl StorageValue for u8
[src]

[src]

[src]

[src]

impl StorageValue for u16
[src]

[src]

[src]

[src]

impl StorageValue for u32
[src]

[src]

[src]

[src]

impl StorageValue for u64
[src]

[src]

[src]

[src]

impl StorageValue for i8
[src]

[src]

[src]

[src]

impl StorageValue for i16
[src]

[src]

[src]

[src]

impl StorageValue for i32
[src]

[src]

[src]

[src]

impl StorageValue for i64
[src]

[src]

[src]

[src]

impl StorageValue for Vec<u8>
[src]

[src]

[src]

[src]

impl StorageValue for String
[src]

[src]

[src]

[src]

Implementors