Trait scale_info::prelude::hash::Hash

1.0.0 · source · []
pub trait Hash {
    fn hash<H>(&self, state: &mut H)
    where
        H: Hasher
; fn hash_slice<H>(data: &[Self], state: &mut H)
    where
        H: Hasher
, { ... } }
Expand description

A hashable type.

Types implementing Hash are able to be hashed with an instance of Hasher.

Implementing Hash

You can derive Hash with #[derive(Hash)] if all fields implement Hash. The resulting hash will be the combination of the values from calling hash on each field.

#[derive(Hash)]
struct Rustacean {
    name: String,
    country: String,
}

If you need more control over how a value is hashed, you can of course implement the Hash trait yourself:

use std::hash::{Hash, Hasher};

struct Person {
    id: u32,
    name: String,
    phone: u64,
}

impl Hash for Person {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.id.hash(state);
        self.phone.hash(state);
    }
}

Hash and Eq

When implementing both Hash and Eq, it is important that the following property holds:

k1 == k2 -> hash(k1) == hash(k2)

In other words, if two keys are equal, their hashes must also be equal. HashMap and HashSet both rely on this behavior.

Thankfully, you won’t need to worry about upholding this property when deriving both Eq and Hash with #[derive(PartialEq, Eq, Hash)].

Prefix collisions

Implementations of hash should ensure that the data they pass to the Hasher are prefix-free. That is, unequal values should cause two different sequences of values to be written, and neither of the two sequences should be a prefix of the other.

For example, the standard implementation of Hash for &str passes an extra 0xFF byte to the Hasher so that the values ("ab", "c") and ("a", "bc") hash differently.

Portability

Due to differences in endianness and type sizes, data fed by Hash to a Hasher should not be considered portable across platforms. Additionally the data passed by most standard library types should not be considered stable between compiler versions.

This means tests shouldn’t probe hard-coded hash values or data fed to a Hasher and instead should check consistency with Eq.

Serialization formats intended to be portable between platforms or compiler versions should either avoid encoding hashes or only rely on Hash and Hasher implementations that provide additional guarantees.

Required methods

Feeds this value into the given Hasher.

Examples
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

let mut hasher = DefaultHasher::new();
7920.hash(&mut hasher);
println!("Hash is {:x}!", hasher.finish());

Provided methods

Feeds a slice of this type into the given Hasher.

This method is meant as a convenience, but its implementation is also explicitly left unspecified. It isn’t guaranteed to be equivalent to repeated calls of hash and implementations of Hash should keep that in mind and call hash themselves if the slice isn’t treated as a whole unit in the PartialEq implementation.

For example, a VecDeque implementation might naïvely call as_slices and then hash_slice on each slice, but this is wrong since the two slices can change with a call to make_contiguous without affecting the PartialEq result. Since these slices aren’t treated as singular units, and instead part of a larger deque, this method cannot be used.

Examples
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

let mut hasher = DefaultHasher::new();
let numbers = [6, 28, 496, 8128];
Hash::hash_slice(&numbers, &mut hasher);
println!("Hash is {:x}!", hasher.finish());

Implementations on Foreign Types

The hash of an array is the same as that of the corresponding slice, as required by the Borrow implementation.

#![feature(build_hasher_simple_hash_one)]
use std::hash::BuildHasher;

let b = std::collections::hash_map::RandomState::new();
let a: [u8; 3] = [0xa8, 0x3c, 0x09];
let s: &[u8] = &[0xa8, 0x3c, 0x09];
assert_eq!(b.hash_one(a), b.hash_one(s));

Writes the contents of the BitSlice, in semantic bit order, into a hasher.

Implementors

The hash of a vector is the same as that of the corresponding slice, as required by the core::borrow::Borrow implementation.

#![feature(build_hasher_simple_hash_one)]
use std::hash::BuildHasher;

let b = std::collections::hash_map::RandomState::new();
let v: Vec<u8> = vec![0xa8, 0x3c, 0x09];
let s: &[u8] = &[0xa8, 0x3c, 0x09];
assert_eq!(b.hash_one(v), b.hash_one(s));

impl Hash for Match

impl<T, const CAP: usize> Hash for ArrayVec<T, CAP> where
    T: Hash

impl<const CAP: usize> Hash for ArrayString<CAP>

impl<O, V> Hash for BitArray<O, V> where
    O: BitOrder,
    V: BitView

impl<R: Hash> Hash for BitIdx<R> where
    R: BitRegister

impl<R: Hash> Hash for BitIdxError<R> where
    R: BitRegister

impl<R: Hash> Hash for BitTail<R> where
    R: BitRegister

impl<R: Hash> Hash for BitPos<R> where
    R: BitRegister

impl<R: Hash> Hash for BitSel<R> where
    R: BitRegister

impl<R: Hash> Hash for BitMask<R> where
    R: BitRegister

impl Hash for Lsb0

impl Hash for Msb0

impl<M, T> Hash for Address<M, T> where
    M: Mutability,
    T: BitStore

impl<T: Hash> Hash for AddressError<T> where
    T: BitStore

impl<M, O, T> Hash for BitRef<'_, M, O, T> where
    M: Mutability,
    O: BitOrder,
    T: BitStore

impl<M, O, T> Hash for BitPtrRange<M, O, T> where
    M: Mutability,
    O: BitOrder,
    T: BitStore

impl<M, O, T> Hash for BitPtr<M, O, T> where
    M: Mutability,
    O: BitOrder,
    T: BitStore

impl<T: Hash> Hash for BitPtrError<T> where
    T: BitStore,
    T::Mem: Hash

impl<O, T> Hash for BitSlice<O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> Hash for BitBox<O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> Hash for BitVec<O, T> where
    O: BitOrder,
    T: BitStore

impl Hash for BigEndian

impl<T: Hash> Hash for LocalResult<T>

impl Hash for FixedOffset

impl Hash for NaiveDate

impl Hash for NaiveTime

impl<Tz: TimeZone> Hash for Date<Tz>

impl<Tz: TimeZone> Hash for DateTime<Tz>

impl Hash for Weekday

impl Hash for Month

impl Hash for Scalar

impl<L: Hash, R: Hash> Hash for Either<L, R>

impl Hash for PollNext

impl<T: Hash> Hash for AllowStdIo<T>

impl<T: Hash, N> Hash for GenericArray<T, N> where
    N: ArrayLength<T>, 

impl Hash for Format

impl Hash for Encoding

impl Hash for Register

impl<T: Hash> Hash for DebugAbbrevOffset<T>

impl<T: Hash> Hash for DebugInfoOffset<T>

impl<T: Hash> Hash for LocationListsOffset<T>

impl<T: Hash> Hash for DebugMacinfoOffset<T>

impl<T: Hash> Hash for DebugMacroOffset<T>

impl<T: Hash> Hash for RawRangeListsOffset<T>

impl<T: Hash> Hash for RangeListsOffset<T>

impl<T: Hash> Hash for DebugTypesOffset<T>

impl<T: Hash> Hash for DebugFrameOffset<T>

impl<T: Hash> Hash for EhFrameOffset<T>

impl<T: Hash> Hash for UnitSectionOffset<T>

impl Hash for SectionId

impl Hash for DwoId

impl Hash for DwSect

impl Hash for DwSectV2

impl Hash for DwUt

impl Hash for DwCfa

impl Hash for DwChildren

impl Hash for DwTag

impl Hash for DwAt

impl Hash for DwForm

impl Hash for DwAte

impl Hash for DwLle

impl Hash for DwDs

impl Hash for DwEnd

impl Hash for DwAccess

impl Hash for DwVis

impl Hash for DwLang

impl Hash for DwAddr

impl Hash for DwId

impl Hash for DwCc

impl Hash for DwInl

impl Hash for DwOrd

impl Hash for DwDsc

impl Hash for DwIdx

impl Hash for DwDefaulted

impl Hash for DwLns

impl Hash for DwLne

impl Hash for DwLnct

impl Hash for DwMacro

impl Hash for DwRle

impl Hash for DwOp

impl Hash for DwEhPe

impl Hash for BigEndian

impl<'input, Endian: Hash> Hash for EndianSlice<'input, Endian> where
    Endian: Endianity

impl<R: Hash + Reader> Hash for LocationListEntry<R>

impl<R: Hash + Reader> Hash for Expression<R>

impl Hash for Range

impl<T: Hash> Hash for UnitOffset<T>

impl Hash for Level

impl Hash for LevelFilter

impl<'a> Hash for Metadata<'a>

impl<'a> Hash for MetadataBuilder<'a>

impl Hash for TDEFLFlush

impl Hash for TDEFLStatus

impl Hash for TINFLStatus

impl Hash for MZFlush

impl Hash for MZStatus

impl Hash for MZError

impl Hash for DataFormat

impl<T: Hash + Scalar> Hash for X<T>

impl<T: Hash + Scalar> Hash for XY<T>

impl<T: Hash + Scalar> Hash for XYZ<T>

impl<T: Hash + Scalar> Hash for XYZW<T>

impl<T: Hash + Scalar> Hash for XYZWA<T>

impl<T: Hash + Scalar> Hash for XYZWAB<T>

impl<T: Hash + Scalar> Hash for IJKW<T>

impl<T: Hash + Scalar> Hash for M2x2<T>

impl<T: Hash + Scalar> Hash for M2x3<T>

impl<T: Hash + Scalar> Hash for M2x4<T>

impl<T: Hash + Scalar> Hash for M2x5<T>

impl<T: Hash + Scalar> Hash for M2x6<T>

impl<T: Hash + Scalar> Hash for M3x2<T>

impl<T: Hash + Scalar> Hash for M3x3<T>

impl<T: Hash + Scalar> Hash for M3x4<T>

impl<T: Hash + Scalar> Hash for M3x5<T>

impl<T: Hash + Scalar> Hash for M3x6<T>

impl<T: Hash + Scalar> Hash for M4x2<T>

impl<T: Hash + Scalar> Hash for M4x3<T>

impl<T: Hash + Scalar> Hash for M4x4<T>

impl<T: Hash + Scalar> Hash for M4x5<T>

impl<T: Hash + Scalar> Hash for M4x6<T>

impl<T: Hash + Scalar> Hash for M5x2<T>

impl<T: Hash + Scalar> Hash for M5x3<T>

impl<T: Hash + Scalar> Hash for M5x4<T>

impl<T: Hash + Scalar> Hash for M5x5<T>

impl<T: Hash + Scalar> Hash for M5x6<T>

impl<T: Hash + Scalar> Hash for M6x2<T>

impl<T: Hash + Scalar> Hash for M6x3<T>

impl<T: Hash + Scalar> Hash for M6x4<T>

impl<T: Hash + Scalar> Hash for M6x5<T>

impl<T: Hash + Scalar> Hash for M6x6<T>

impl<const R: usize> Hash for Const<R>

impl<T: Hash, const R: usize, const C: usize> Hash for ArrayStorage<T, R, C>

impl<T, R, C, S> Hash for Matrix<T, R, C, S> where
    T: Scalar + Hash,
    R: Dim,
    C: Dim,
    S: Storage<T, R, C>, 

impl<T: Hash> Hash for Unit<T>

impl<T: Scalar + Hash, const D: usize> Hash for Point<T, D>

impl<T: Scalar + Hash, const D: usize> Hash for Rotation<T, D> where
    <DefaultAllocator as Allocator<T, Const<D>, Const<D>>>::Buffer: Hash

impl<T: Scalar + Hash> Hash for Quaternion<T>

impl<T: Scalar + Hash, const D: usize> Hash for Translation<T, D> where
    Owned<T, Const<D>>: Hash

impl<T: Scalar + Hash, R: Hash, const D: usize> Hash for Isometry<T, R, D> where
    Owned<T, Const<D>>: Hash

impl<T: Scalar + Hash, R: Hash, const D: usize> Hash for Similarity<T, R, D> where
    Owned<T, Const<D>>: Hash

impl Hash for TGeneral

impl Hash for TProjective

impl Hash for TAffine

impl<T: Hash> Hash for Complex<T>

impl<T: Clone + Integer + Hash> Hash for Ratio<T>

impl Hash for AddressSize

impl Hash for SectionKind

impl Hash for ComdatKind

impl Hash for SymbolKind

impl Hash for SymbolScope

impl Hash for FileFlags

impl<Section: Hash> Hash for SymbolFlags<Section>

impl Hash for Endianness

impl Hash for BigEndian

impl<E: Hash + Endian> Hash for U16Bytes<E>

impl<E: Hash + Endian> Hash for U32Bytes<E>

impl<E: Hash + Endian> Hash for U64Bytes<E>

impl<E: Hash + Endian> Hash for I16Bytes<E>

impl<E: Hash + Endian> Hash for I32Bytes<E>

impl<E: Hash + Endian> Hash for I64Bytes<E>

impl Hash for ArchiveKind

impl Hash for FileKind

impl Hash for ObjectKind

impl Hash for SymbolIndex

impl<'data> Hash for SymbolMapName<'data>

impl<'data> Hash for ObjectMapEntry<'data>

impl<'data> Hash for CompressedData<'data>

impl Hash for Type

impl Hash for ValueType

impl Hash for BlockType

impl Hash for Instruction

impl Hash for BrTableData

impl Hash for U128

impl Hash for U256

impl Hash for U512

impl Hash for H128

impl Hash for H160

impl Hash for H256

impl Hash for H512

impl Hash for Ident

impl Hash for PublicKey

impl Hash for VRFOutput

impl Hash for VRFInOut

impl Hash for ChainCode

impl<K: Hash> Hash for ExtendedKey<K>

impl Hash for Number

impl<A: Array> Hash for SmallVec<A> where
    A::Item: Hash

impl Hash for Public

impl Hash for Signature

impl Hash for Public

impl Hash for Signature

impl Hash for Public

impl Hash for Signature

impl Hash for AccountId32

impl Hash for Dummy

impl Hash for KeyTypeId

impl Hash for Public

impl Hash for Signature

impl Hash for Public

impl Hash for Signature

impl Hash for Public

impl Hash for Signature

impl Hash for Bytes

impl<AccountId: Hash, AccountIndex: Hash> Hash for MultiAddress<AccountId, AccountIndex>

impl Hash for StorageKey

impl Hash for StorageData

impl Hash for ChildInfo

impl Hash for ParseError

impl Hash for Underscore

impl Hash for Abstract

impl Hash for As

impl Hash for Async

impl Hash for Auto

impl Hash for Await

impl Hash for Become

impl Hash for Box

impl Hash for Break

impl Hash for Const

impl Hash for Continue

impl Hash for Crate

impl Hash for Default

impl Hash for Do

impl Hash for Dyn

impl Hash for Else

impl Hash for Enum

impl Hash for Extern

impl Hash for Final

impl Hash for Fn

impl Hash for For

impl Hash for If

impl Hash for Impl

impl Hash for In

impl Hash for Let

impl Hash for Loop

impl Hash for Macro

impl Hash for Match

impl Hash for Mod

impl Hash for Move

impl Hash for Mut

impl Hash for Override

impl Hash for Priv

impl Hash for Pub

impl Hash for Ref

impl Hash for Return

impl Hash for SelfType

impl Hash for SelfValue

impl Hash for Static

impl Hash for Struct

impl Hash for Super

impl Hash for Trait

impl Hash for Try

impl Hash for Type

impl Hash for Typeof

impl Hash for Union

impl Hash for Unsafe

impl Hash for Unsized

impl Hash for Use

impl Hash for Virtual

impl Hash for Where

impl Hash for While

impl Hash for Yield

impl Hash for Add

impl Hash for AddEq

impl Hash for And

impl Hash for AndAnd

impl Hash for AndEq

impl Hash for At

impl Hash for Bang

impl Hash for Caret

impl Hash for CaretEq

impl Hash for Colon

impl Hash for Colon2

impl Hash for Comma

impl Hash for Div

impl Hash for DivEq

impl Hash for Dollar

impl Hash for Dot

impl Hash for Dot2

impl Hash for Dot3

impl Hash for DotDotEq

impl Hash for Eq

impl Hash for EqEq

impl Hash for Ge

impl Hash for Gt

impl Hash for Le

impl Hash for Lt

impl Hash for MulEq

impl Hash for Ne

impl Hash for Or

impl Hash for OrEq

impl Hash for OrOr

impl Hash for Pound

impl Hash for Question

impl Hash for RArrow

impl Hash for LArrow

impl Hash for Rem

impl Hash for RemEq

impl Hash for FatArrow

impl Hash for Semi

impl Hash for Shl

impl Hash for ShlEq

impl Hash for Shr

impl Hash for ShrEq

impl Hash for Star

impl Hash for Sub

impl Hash for SubEq

impl Hash for Tilde

impl Hash for Brace

impl Hash for Bracket

impl Hash for Paren

impl Hash for Group

impl Hash for Member

impl Hash for Index

impl<'a> Hash for ImplGenerics<'a>

impl<'a> Hash for TypeGenerics<'a>

impl<'a> Hash for Turbofish<'a>

impl Hash for Lifetime

impl Hash for LitStr

impl Hash for LitByteStr

impl Hash for LitByte

impl Hash for LitChar

impl Hash for LitInt

impl Hash for LitFloat

impl<T, P> Hash for Punctuated<T, P> where
    T: Hash,
    P: Hash

impl Hash for Abi

impl Hash for Arm

impl Hash for AttrStyle

impl Hash for Attribute

impl Hash for BareFnArg

impl Hash for BinOp

impl Hash for Binding

impl Hash for Block

impl Hash for ConstParam

impl Hash for Constraint

impl Hash for Data

impl Hash for DataEnum

impl Hash for DataStruct

impl Hash for DataUnion

impl Hash for DeriveInput

impl Hash for Expr

impl Hash for ExprArray

impl Hash for ExprAssign

impl Hash for ExprAsync

impl Hash for ExprAwait

impl Hash for ExprBinary

impl Hash for ExprBlock

impl Hash for ExprBox

impl Hash for ExprBreak

impl Hash for ExprCall

impl Hash for ExprCast

impl Hash for ExprClosure

impl Hash for ExprField

impl Hash for ExprForLoop

impl Hash for ExprGroup

impl Hash for ExprIf

impl Hash for ExprIndex

impl Hash for ExprLet

impl Hash for ExprLit

impl Hash for ExprLoop

impl Hash for ExprMacro

impl Hash for ExprMatch

impl Hash for ExprParen

impl Hash for ExprPath

impl Hash for ExprRange

impl Hash for ExprRepeat

impl Hash for ExprReturn

impl Hash for ExprStruct

impl Hash for ExprTry

impl Hash for ExprTuple

impl Hash for ExprType

impl Hash for ExprUnary

impl Hash for ExprUnsafe

impl Hash for ExprWhile

impl Hash for ExprYield

impl Hash for Field

impl Hash for FieldPat

impl Hash for FieldValue

impl Hash for Fields

impl Hash for FieldsNamed

impl Hash for File

impl Hash for FnArg

impl Hash for ForeignItem

impl Hash for Generics

impl Hash for ImplItem

impl Hash for Item

impl Hash for ItemConst

impl Hash for ItemEnum

impl Hash for ItemFn

impl Hash for ItemImpl

impl Hash for ItemMacro

impl Hash for ItemMacro2

impl Hash for ItemMod

impl Hash for ItemStatic

impl Hash for ItemStruct

impl Hash for ItemTrait

impl Hash for ItemType

impl Hash for ItemUnion

impl Hash for ItemUse

impl Hash for Label

impl Hash for LifetimeDef

impl Hash for Lit

impl Hash for LitBool

impl Hash for Local

impl Hash for Macro

impl Hash for Meta

impl Hash for MetaList

impl Hash for NestedMeta

impl Hash for Pat

impl Hash for PatBox

impl Hash for PatIdent

impl Hash for PatLit

impl Hash for PatMacro

impl Hash for PatOr

impl Hash for PatPath

impl Hash for PatRange

impl Hash for PatRest

impl Hash for PatSlice

impl Hash for PatStruct

impl Hash for PatTuple

impl Hash for PatType

impl Hash for PatWild

impl Hash for Path

impl Hash for PathSegment

impl Hash for PredicateEq

impl Hash for QSelf

impl Hash for RangeLimits

impl Hash for Receiver

impl Hash for ReturnType

impl Hash for Signature

impl Hash for Stmt

impl Hash for TraitBound

impl Hash for TraitItem

impl Hash for Type

impl Hash for TypeArray

impl Hash for TypeBareFn

impl Hash for TypeGroup

impl Hash for TypeInfer

impl Hash for TypeMacro

impl Hash for TypeNever

impl Hash for TypeParam

impl Hash for TypeParen

impl Hash for TypePath

impl Hash for TypePtr

impl Hash for TypeSlice

impl Hash for TypeTuple

impl Hash for UnOp

impl Hash for UseGlob

impl Hash for UseGroup

impl Hash for UseName

impl Hash for UsePath

impl Hash for UseRename

impl Hash for UseTree

impl Hash for Variadic

impl Hash for Variant

impl Hash for VisCrate

impl Hash for VisPublic

impl Hash for Visibility

impl Hash for WhereClause

impl Hash for AddBounds

impl Hash for BindStyle

impl<'a> Hash for BindingInfo<'a>

impl<'a> Hash for VariantAst<'a>

impl<'a> Hash for VariantInfo<'a>

impl<'a> Hash for Structure<'a>

impl<A: Array> Hash for ArrayVec<A> where
    A::Item: Hash

impl<'s, T> Hash for SliceVec<'s, T> where
    T: Hash

impl<A: Array> Hash for TinyVec<A> where
    A::Item: Hash

impl<T: Hash> Hash for Spanned<T>

impl Hash for Span

impl Hash for Identifier

impl Hash for Field

impl Hash for Level

impl Hash for LevelFilter

impl Hash for Id

impl Hash for B0

impl Hash for B1

impl<U: Hash + Unsigned + NonZero> Hash for PInt<U>

impl<U: Hash + Unsigned + NonZero> Hash for NInt<U>

impl Hash for Z0

impl Hash for UTerm

impl<U: Hash, B: Hash> Hash for UInt<U, B>

impl Hash for ATerm

impl<V: Hash, A: Hash> Hash for TArr<V, A>

impl Hash for Greater

impl Hash for Less

impl Hash for Equal