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()iffa == 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()iffa <= 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§
Sourcetype Bytes<'a>: AsRef<[u8]>
where
Self: 'a
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§
Sourcefn to_orderable_bytes(&self) -> Self::Bytes<'_>
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
impl OrderableBytes for String
Source§impl OrderableBytes for Vec<u8>
impl OrderableBytes for Vec<u8>
Source§impl OrderableBytes for [u8]
impl OrderableBytes for [u8]
Source§impl OrderableBytes for bool
impl OrderableBytes for bool
Source§impl OrderableBytes for char
impl OrderableBytes for char
Source§impl OrderableBytes for f32
impl OrderableBytes for f32
Source§impl OrderableBytes for f64
impl OrderableBytes for f64
Source§impl OrderableBytes for i8
impl OrderableBytes for i8
Source§impl OrderableBytes for i16
impl OrderableBytes for i16
Source§impl OrderableBytes for i32
impl OrderableBytes for i32
Source§impl OrderableBytes for i64
impl OrderableBytes for i64
Source§impl OrderableBytes for i128
impl OrderableBytes for i128
Source§impl OrderableBytes for str
impl OrderableBytes for str
Source§impl OrderableBytes for u8
impl OrderableBytes for u8
Source§impl OrderableBytes for u16
impl OrderableBytes for u16
Source§impl OrderableBytes for u32
impl OrderableBytes for u32
Source§impl OrderableBytes for u64
impl OrderableBytes for u64
Source§impl OrderableBytes for u128
impl OrderableBytes for u128
Source§impl<T: OrderableBytes + ?Sized> OrderableBytes for &T
A reference encodes exactly as the value it points to.
impl<T: OrderableBytes + ?Sized> OrderableBytes for &T
A reference encodes exactly as the value it points to.
type Bytes<'a> = <T as OrderableBytes>::Bytes<'a> where Self: 'a
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>.
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>.