Trait exonum::storage::StorageKey [] [src]

pub trait StorageKey {
    fn size(&self) -> usize;
fn write(&self, buffer: &mut [u8]);
fn read(buffer: &[u8]) -> Self; }

A trait that defines serialization of corresponding types as storage keys.

Since internally the keys are sorted in a serialized form, the big-endian encoding is used.

Examples

Implementing StorageKey for the type:


use std::mem;

use exonum::storage::StorageKey;
use byteorder::{LittleEndian, ByteOrder};

struct Key {
    a: i16,
    b: u32,
}

impl StorageKey for Key {
    fn size(&self) -> usize {
        mem::size_of_val(&self.a) + mem::size_of_val(&self.b)
    }

    fn write(&self, buffer: &mut [u8]) {
        LittleEndian::write_i16(&mut buffer[0..2], self.a);
        LittleEndian::write_u32(&mut buffer[2..6], self.b);
    }

    fn read(buffer: &[u8]) -> Self {
        let a = LittleEndian::read_i16(&buffer[0..2]);
        let b = LittleEndian::read_u32(&buffer[2..6]);
        Key { a, b }
    }
}

Required Methods

Returns the size of the serialized key in bytes.

Serialize a key into the specified buffer of bytes.

The caller must guarantee that the size of the buffer is equal to the precalculated size of the serialized key.

Deserialize a key from the specified buffer of bytes.

Implementations on Foreign Types

impl StorageKey for ()
[src]

[src]

[src]

[src]

impl StorageKey for u8
[src]

[src]

[src]

[src]

impl StorageKey for u16
[src]

[src]

[src]

[src]

impl StorageKey for u32
[src]

[src]

[src]

[src]

impl StorageKey for u64
[src]

[src]

[src]

[src]

impl StorageKey for i8
[src]

[src]

[src]

[src]

impl StorageKey for i16
[src]

[src]

[src]

[src]

impl StorageKey for i32
[src]

[src]

[src]

[src]

impl StorageKey for i64
[src]

[src]

[src]

[src]

impl StorageKey for Vec<u8>
[src]

[src]

[src]

[src]

impl StorageKey for String
[src]

[src]

[src]

[src]

impl StorageKey for [u8; 32]
[src]

[src]

[src]

[src]

Implementors