Trait blart::bytes::ordered::BytesMapping
source · pub unsafe trait BytesMapping {
type Domain;
type Bytes: AsRef<[u8]>;
// Required methods
fn to_bytes(value: Self::Domain) -> Self::Bytes;
fn from_bytes(bytes: Self::Bytes) -> Self::Domain;
}
Expand description
Trait representing a reversible conversion from a type to some sort of byte string, while preserving the ordering of the original type.
The ordering of the original type is determined by the Ord
implementation, and the ordering of the byte string type is the
lexicographic ordering.
The mapping should also maintain equality for the PartialEq
and Eq
implementations, along with hashing for the Hash
implementation.
The following property should hold true:
value_a == value_b -> Mapping::to_bytes(value_a) == Mapping::to_bytes(value_b)
And this implies that the hash
implementations should also match, in line
with the Hash
and Eq
documentation.
Safety
This trait is unsafe because implementing it implies that the
Mapped<Self>
type will implement OrderedBytes
, so the safety
requirements must be upheld. Namely, that the ordering of values of the
Self::Domain
type must be equal to the ordering of those same values
translated to the the Self::Bytes
type.
Required Associated Types§
sourcetype Bytes: AsRef<[u8]>
type Bytes: AsRef<[u8]>
The bytestring type that the Self::Domain
is converted to.
Required Methods§
sourcefn to_bytes(value: Self::Domain) -> Self::Bytes
fn to_bytes(value: Self::Domain) -> Self::Bytes
Convert the domain type into the bytestring type
sourcefn from_bytes(bytes: Self::Bytes) -> Self::Domain
fn from_bytes(bytes: Self::Bytes) -> Self::Domain
Convert the bytestring type back into the domain type