Module std::cmp
Equality, ordering and hashing: Eq, Ord and Hash, the traits std::map and the
sorted collections bound their keys by, with impls for the scalars, Box, Option, Buf,
Str and &[u8], and named derives (impl Eq for Key = DeriveEq;) for your own types.
Source: std/cmp.weir; committed interface: std/cmp.iface. Index
- enum
Order: The result of comparing two values withOrd::cmp. - trait
Eq: Equality.eqmust be reflexive, symmetric and transitive; hashed maps rely on it. - trait
Ord: A total order, consistent withEq:cmpreturnsEqualexactly wheneqistrue. - trait
Hash: Hashing into aHasher. Values equal byEqmust feed the hasher the same words. - struct
Hasher: A 64-bit hasher (rapidhash), fed one word at a time; no allocation, no capability. - impl
DeriveEq: Derived equality: same variant, then every field equal. Install it in the type's module withimpl Eq for Key = DeriveEq;; every field type must beEq. - impl
DeriveOrd: Derived order: variants in declaration order, then fields in order, byOrd. Install withimpl Ord for Key = DeriveOrd;. - impl
DeriveHash: Derived hash: the variant's index, then each field'sHash. Install withimpl Hash for Key = DeriveHash;.
enum Order
pub enum Order: copy {
Less,
Equal,
Greater,
}
The result of comparing two values with Ord::cmp.
Variants:
trait Eq
pub trait Eq {
fn eq(&self, o: &Self) -> Bool;
}
Equality. eq must be reflexive, symmetric and transitive; hashed maps rely on it.
Implementations:
impl Eq for Intimpl Eq for u8impl Eq for u16impl Eq for u32impl Eq for u64impl Eq for i8impl Eq for i16impl Eq for i32impl Eq for i64impl Eq for usizeimpl Eq for isizeimpl Eq for Boolimpl<T: Eq> Eq for Box<T>impl<T: Eq> Eq for Option<T>impl Eq for Strimpl Eq for &[u8]impl<T: Eq> Eq for Buf<T>
method eq
fn eq(&self, o: &Self) -> Bool;
true when self and o are equal.
trait Ord
pub trait Ord {
fn cmp(&self, o: &Self) -> Order;
}
A total order, consistent with Eq: cmp returns Equal exactly when eq is true.
Implementations:
impl Ord for Intimpl Ord for u8impl Ord for u16impl Ord for u32impl Ord for u64impl Ord for i8impl Ord for i16impl Ord for i32impl Ord for i64impl Ord for usizeimpl Ord for isizeimpl Ord for Boolimpl<T: Ord> Ord for Box<T>impl<T: Ord> Ord for Option<T>impl Ord for Strimpl Ord for &[u8]impl<T: Ord> Ord for Buf<T>
method cmp
fn cmp(&self, o: &Self) -> Order;
How self compares to o.
trait Hash
pub trait Hash {
fn hash(&self, h: &mut Hasher);
}
Hashing into a Hasher. Values equal by Eq must feed the hasher the same words.
Implementations:
impl Hash for Intimpl Hash for u8impl Hash for u16impl Hash for u32impl Hash for u64impl Hash for i8impl Hash for i16impl Hash for i32impl Hash for i64impl Hash for usizeimpl Hash for isizeimpl Hash for Boolimpl<T: Hash> Hash for Box<T>impl<T: Hash> Hash for Option<T>impl Hash for Strimpl Hash for &[u8]impl<T: Hash> Hash for Buf<T>
method hash
fn hash(&self, h: &mut Hasher);
Feeds self into h.
struct Hasher
pub struct Hasher: copy {
/* private fields */
}
A 64-bit hasher (rapidhash), fed one word at a time; no allocation, no capability.
The same words give the same hash interpreted, native and on every target. It is not
cryptographic: Hasher::new uses fixed secrets, fine for trusted keys and simulation;
for keys an attacker chooses, key it with Hasher::with_seed and a secret seed drawn from
a Random capability, so that colliding keys cannot be built without seeing hashes.
impl Hasher
method new
pub fn new() -> Hasher
A hasher with fixed seed and secrets: the same hashes on every run, so deterministic, and open to keys built to collide.
Committed interface (std/cmp.iface): the signature alone.
method with_seed
pub fn with_seed(seed: u64) -> Hasher
A hasher whose seed and secret derive from seed (rapidhash's RapidSecrets::seed);
a secret seed, drawn from the Random capability, keeps an attacker from building
colliding keys.
Committed interface (std/cmp.iface): the signature alone.
method write_u64
pub fn write_u64(&mut self, x: u64)
Adds the word x to the state.
Committed interface (std/cmp.iface):
writes through self: {hi, lo, n, seed}
method finish
pub fn finish(&self) -> u64
The hash of the words written so far; the hasher stays usable.
Committed interface (std/cmp.iface): the signature alone.
impl DeriveEq
impl DeriveEq<T: reflect(Eq)> Eq for T
Derived equality: same variant, then every field equal. Install it in the type's module with
impl Eq for Key = DeriveEq;; every field type must be Eq.
impl DeriveOrd
impl DeriveOrd<T: reflect(Ord)> Ord for T
Derived order: variants in declaration order, then fields in order, by Ord.
Install with impl Ord for Key = DeriveOrd;.
impl DeriveHash
impl DeriveHash<T: reflect(Hash)> Hash for T
Derived hash: the variant's index, then each field's Hash. Install with
impl Hash for Key = DeriveHash;.