Trait exonum::storage::StorageKey
[−]
[src]
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.
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 SystemTime
[src]
The SystemTime
(de)serialization is only possible for the values greater than
1970-01-01 00:00:00 UTC. Since the SystemTime
type saves time in the form
(seconds: u64, nanoseconds: u32)
, the total occupied size of SystemTime
in the storage
is 12 bytes. The seconds are stored in the first 8 bytes as per the StorageKey
implementation
for u64, and nanoseconds in the remaining 4 bytes as per the StorageKey
implementation for u32.