Trait matterdb::BinaryKey[][src]

pub trait BinaryKey: ToOwned {
    fn size(&self) -> usize;
fn write(&self, buffer: &mut [u8]) -> usize;
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 matterdb::BinaryKey;

#[derive(Clone)]
struct Key {
    a: i16,
    b: u32,
}

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

    fn write(&self, buffer: &mut [u8]) -> usize {
        self.a.write(&mut buffer[0..2]);
        self.b.write(&mut buffer[2..6]);
        self.size()
    }

    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[src]

Returns the size of the serialized key in bytes.

fn write(&self, buffer: &mut [u8]) -> usize[src]

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 returned via size(). Returns number of written bytes. The provided buffer may be uninitialized; an implementor must not read from it.

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

Deserializes the key from the specified buffer of bytes.

Loading content...

Implementations on Foreign Types

impl BinaryKey for ()[src]

No-op implementation.

fn size(&self) -> usize[src]

fn write(&self, _buffer: &mut [u8]) -> usize[src]

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

impl BinaryKey for u8[src]

fn size(&self) -> usize[src]

fn write(&self, buffer: &mut [u8]) -> usize[src]

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

impl BinaryKey for i8[src]

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

fn size(&self) -> usize[src]

fn write(&self, buffer: &mut [u8]) -> usize[src]

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

impl BinaryKey for u16[src]

Uses big-endian encoding.

fn size(&self) -> usize[src]

fn write(&self, buffer: &mut [u8]) -> usize[src]

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

impl BinaryKey for i16[src]

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

fn size(&self) -> usize[src]

fn write(&self, buffer: &mut [u8]) -> usize[src]

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

impl BinaryKey for u32[src]

Uses big-endian encoding.

fn size(&self) -> usize[src]

fn write(&self, buffer: &mut [u8]) -> usize[src]

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

impl BinaryKey for i32[src]

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

fn size(&self) -> usize[src]

fn write(&self, buffer: &mut [u8]) -> usize[src]

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

impl BinaryKey for u64[src]

Uses big-endian encoding.

fn size(&self) -> usize[src]

fn write(&self, buffer: &mut [u8]) -> usize[src]

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

impl BinaryKey for i64[src]

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

fn size(&self) -> usize[src]

fn write(&self, buffer: &mut [u8]) -> usize[src]

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

impl BinaryKey for u128[src]

Uses big-endian encoding.

fn size(&self) -> usize[src]

fn write(&self, buffer: &mut [u8]) -> usize[src]

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

impl BinaryKey for i128[src]

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

fn size(&self) -> usize[src]

fn write(&self, buffer: &mut [u8]) -> usize[src]

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

impl BinaryKey for Vec<u8>[src]

fn size(&self) -> usize[src]

fn write(&self, buffer: &mut [u8]) -> usize[src]

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

impl BinaryKey for [u8][src]

fn size(&self) -> usize[src]

fn write(&self, buffer: &mut [u8]) -> usize[src]

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

impl BinaryKey for [u8; 32][src]

fn size(&self) -> usize[src]

fn write(&self, buffer: &mut [u8]) -> usize[src]

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

impl BinaryKey for String[src]

Uses UTF-8 string serialization.

fn size(&self) -> usize[src]

fn write(&self, buffer: &mut [u8]) -> usize[src]

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

impl BinaryKey for str[src]

fn size(&self) -> usize[src]

fn write(&self, buffer: &mut [u8]) -> usize[src]

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

impl BinaryKey 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 BinaryKey implementation for i64, and nanoseconds, which are stored in the remaining 4 bytes as per the BinaryKey implementation for u32.

fn size(&self) -> usize[src]

fn write(&self, buffer: &mut [u8]) -> usize[src]

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

impl BinaryKey for Uuid[src]

fn size(&self) -> usize[src]

fn write(&self, buffer: &mut [u8]) -> usize[src]

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

impl BinaryKey for Decimal[src]

fn size(&self) -> usize[src]

fn write(&self, buffer: &mut [u8]) -> usize[src]

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

Loading content...

Implementors

Loading content...