Skip to main content

OrderableBytes

Trait OrderableBytes 

Source
pub trait OrderableBytes: Sealed {
    type Bytes<'a>: AsRef<[u8]>
       where Self: 'a;

    // Required method
    fn to_orderable_bytes(&self) -> Self::Bytes<'_>;
}
Expand description

Maps a value to its canonical, order-preserving byte encoding.

Implementors guarantee, for any a and b of the implementing type:

  • Equality: byte equality of the outputs agrees with the type’s value equality (a.to_orderable_bytes() == b.to_orderable_bytes() iff a == b).
  • Order: byte-wise lexicographic comparison of the outputs agrees with the type’s natural total order (a.to_orderable_bytes() <= b.to_orderable_bytes() iff a <= b).

Every implementor also implements exactly one of FixedOrderableBytes or VariableOrderableBytes. Code that needs a fixed length, such as code that pads to a block size, must bound on FixedOrderableBytes, not on this trait.

The trait is sealed, so types outside this crate can’t implement it:

ⓘ
use orderable_bytes::OrderableBytes;

struct Mine(u32);

impl OrderableBytes for Mine {
    type Bytes<'a> = [u8; 4];
    fn to_orderable_bytes(&self) -> [u8; 4] {
        self.0.to_be_bytes()
    }
}

In 0.1 this trait was called ToOrderableBytes and was fixed-length only. It was renamed so that 0.1 code bounded on it fails to compile rather than silently accepting variable-length types: migrate such bounds to FixedOrderableBytes.

Required Associated Types§

Source

type Bytes<'a>: AsRef<[u8]> where Self: 'a

The bytes returned by to_orderable_bytes: a [u8; ENCODED_LEN] array for fixed-length types, or a slice borrowed from self for variable-length ones, so encoding a string makes no copy of it.

Because it may borrow from self, it can’t outlive the value. To keep fixed-length bytes, use FixedOrderableBytes::to_fixed_orderable_bytes.

Required Methods§

Source

fn to_orderable_bytes(&self) -> Self::Bytes<'_>

Build the canonical, order-preserving byte encoding of self.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl OrderableBytes for String

Source§

type Bytes<'a> = &'a [u8]

Source§

fn to_orderable_bytes(&self) -> &[u8] ⓘ

Source§

impl OrderableBytes for Vec<u8>

Source§

type Bytes<'a> = &'a [u8]

Source§

fn to_orderable_bytes(&self) -> &[u8] ⓘ

Source§

impl OrderableBytes for [u8]

Source§

type Bytes<'a> = &'a [u8]

Source§

fn to_orderable_bytes(&self) -> &[u8] ⓘ

Source§

impl OrderableBytes for bool

Source§

type Bytes<'a> = [u8; 1]

Source§

fn to_orderable_bytes(&self) -> [u8; 1]

Source§

impl OrderableBytes for char

Source§

type Bytes<'a> = [u8; 4]

Source§

fn to_orderable_bytes(&self) -> [u8; 4]

Source§

impl OrderableBytes for f32

Source§

type Bytes<'a> = [u8; 4]

Source§

fn to_orderable_bytes(&self) -> [u8; 4]

Source§

impl OrderableBytes for f64

Source§

type Bytes<'a> = [u8; 8]

Source§

fn to_orderable_bytes(&self) -> [u8; 8]

Source§

impl OrderableBytes for i8

Source§

type Bytes<'a> = [u8; 1]

Source§

fn to_orderable_bytes(&self) -> [u8; 1]

Source§

impl OrderableBytes for i16

Source§

type Bytes<'a> = [u8; 2]

Source§

fn to_orderable_bytes(&self) -> [u8; 2]

Source§

impl OrderableBytes for i32

Source§

type Bytes<'a> = [u8; 4]

Source§

fn to_orderable_bytes(&self) -> [u8; 4]

Source§

impl OrderableBytes for i64

Source§

type Bytes<'a> = [u8; 8]

Source§

fn to_orderable_bytes(&self) -> [u8; 8]

Source§

impl OrderableBytes for i128

Source§

type Bytes<'a> = [u8; 16]

Source§

fn to_orderable_bytes(&self) -> [u8; 16]

Source§

impl OrderableBytes for str

Source§

type Bytes<'a> = &'a [u8]

Source§

fn to_orderable_bytes(&self) -> &[u8] ⓘ

Source§

impl OrderableBytes for u8

Source§

type Bytes<'a> = [u8; 1]

Source§

fn to_orderable_bytes(&self) -> [u8; 1]

Source§

impl OrderableBytes for u16

Source§

type Bytes<'a> = [u8; 2]

Source§

fn to_orderable_bytes(&self) -> [u8; 2]

Source§

impl OrderableBytes for u32

Source§

type Bytes<'a> = [u8; 4]

Source§

fn to_orderable_bytes(&self) -> [u8; 4]

Source§

impl OrderableBytes for u64

Source§

type Bytes<'a> = [u8; 8]

Source§

fn to_orderable_bytes(&self) -> [u8; 8]

Source§

impl OrderableBytes for u128

Source§

type Bytes<'a> = [u8; 16]

Source§

fn to_orderable_bytes(&self) -> [u8; 16]

Source§

impl<T: OrderableBytes + ?Sized> OrderableBytes for &T

A reference encodes exactly as the value it points to.

Source§

type Bytes<'a> = <T as OrderableBytes>::Bytes<'a> where Self: 'a

Source§

fn to_orderable_bytes(&self) -> T::Bytes<'_>

Source§

impl<const N: usize> OrderableBytes for [u8; N]

A byte array encodes to itself: byte-wise order is already the order Rust defines for arrays. Hashes and UUIDs can be encoded without copying them into a Vec<u8>.

Source§

type Bytes<'a> = &'a [u8]

Source§

fn to_orderable_bytes(&self) -> &[u8] ⓘ

Implementors§