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

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:

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:

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:

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;.