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

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

A type that can be (de)serialized as a key in the blockchain storage.

Since keys are sorted in the serialized form, the big-endian encoding should be used with unsigned integer types. Note, however, that the big-endian encoding will not sort signed integer types in the natural order; therefore, they are mapped to the corresponding unsigned type by adding a constant to the source value.

Examples

use std::mem;
use exonum::storage::StorageKey;

#[derive(Clone)]
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]) {
        self.a.write(&mut buffer[0..2]);
        self.b.write(&mut buffer[2..6]);
    }

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

Required methods

fn size(&self) -> usize

Returns the size of the serialized key in bytes.

fn write(&self, buffer: &mut [u8])

Serializes the 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.

fn read(buffer: &[u8]) -> Self::Owned

Deserializes the key from the specified buffer of bytes.

Loading content...

Implementations on Foreign Types

impl StorageKey for ()
[src]

No-op implementation.

impl StorageKey for u8
[src]

impl StorageKey for i8
[src]

Uses encoding with the values mapped to u8 by adding the corresponding constant (128) to the value.

impl StorageKey for u16
[src]

Uses big-endian encoding.

impl StorageKey for i16
[src]

Uses big-endian encoding with the values mapped to the unsigned format by adding the corresponding constant to the value.

impl StorageKey for u32
[src]

Uses big-endian encoding.

impl StorageKey for i32
[src]

Uses big-endian encoding with the values mapped to the unsigned format by adding the corresponding constant to the value.

impl StorageKey for u64
[src]

Uses big-endian encoding.

impl StorageKey for i64
[src]

Uses big-endian encoding with the values mapped to the unsigned format by adding the corresponding constant to the value.

impl StorageKey for Vec<u8>
[src]

impl StorageKey for [u8]
[src]

impl StorageKey for String
[src]

Uses UTF-8 string serialization.

impl StorageKey for str
[src]

impl StorageKey for DateTime<Utc>
[src]

chrono::DateTime uses only 12 bytes in the storage. It is represented by number of seconds since 1970-01-01 00:00:00 UTC, which are stored in the first 8 bytes as per the StorageKey implementation for i64, and nanoseconds, which are stored in the remaining 4 bytes as per the StorageKey implementation for u32.

impl StorageKey for Uuid
[src]

impl StorageKey for Decimal
[src]

Loading content...

Implementors

impl StorageKey for Hash
[src]

impl StorageKey for PublicKey
[src]

impl StorageKey for Signature
[src]

impl StorageKey for ProofPath
[src]

Loading content...