Trait nom::lib::std::hash::Hash1.0.0[][src]

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.

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

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<A> Hash for ArrayString<A> where
    A: Array<Item = u8> + Copy

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

impl<'a> Hash for Utf8Component<'a>

impl<'a> Hash for Utf8Prefix<'a>

impl<'a> Hash for Utf8PrefixComponent<'a>

impl Hash for Utf8PathBuf

impl Hash for Utf8Path

impl Hash for Diagnostic

impl Hash for Artifact

impl Hash for BuildScript

impl Hash for Message

impl Hash for PackageId

impl Hash for Target

impl Hash for CfgExpr

impl Hash for Cfg

impl Hash for Platform

impl Hash for Triple

impl Hash for Arch

impl Hash for Vendor

impl Hash for Os

impl Hash for Family

impl Hash for Env

impl Hash for Endian

impl Hash for TargetInfo

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 FileFormat

impl<T: Hash> Hash for CachePadded<T>

impl<T: Hash + ?Sized> Hash for DebugIgnore<T>

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

impl Hash for WriteStyle

impl Hash for FixedBitSet

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<'g> Hash for BuildTargetId<'g>

impl<'g> Hash for BuildTargetKind<'g>

impl<'g> Hash for FeatureId<'g>

impl Hash for FeatureType

impl<'g> Hash for PackageSource<'g>

impl<'g> Hash for ExternalSource<'g>

impl<'g> Hash for GitReq<'g>

impl<'g> Hash for PackagePublish<'g>

impl Hash for PackageId

impl Hash for Duration

impl<T: Hash> Hash for Serde<T>

impl<A: Hash, B: Hash> Hash for EitherOrBoth<A, B>

impl Hash for group

impl Hash for utimbuf

impl Hash for timeval

impl Hash for timespec

impl Hash for rlimit

impl Hash for rusage

impl Hash for ipv6_mreq

impl Hash for hostent

impl Hash for iovec

impl Hash for pollfd

impl Hash for winsize

impl Hash for linger

impl Hash for sigval

impl Hash for itimerval

impl Hash for tms

impl Hash for servent

impl Hash for protoent

impl Hash for in_addr

impl Hash for ip_mreq

impl Hash for ip_mreqn

impl Hash for sockaddr

impl Hash for sockaddr_in

impl Hash for addrinfo

impl Hash for sockaddr_ll

impl Hash for fd_set

impl Hash for tm

impl Hash for sched_param

impl Hash for Dl_info

impl Hash for lconv

impl Hash for in_pktinfo

impl Hash for ifaddrs

impl Hash for in6_rtmsg

impl Hash for arpreq

impl Hash for arpreq_old

impl Hash for arphdr

impl Hash for mmsghdr

impl Hash for epoll_event

impl Hash for sockaddr_un

impl Hash for utsname

impl Hash for sigevent

impl Hash for rlimit64

impl Hash for glob_t

impl Hash for passwd

impl Hash for spwd

impl Hash for dqblk

impl Hash for itimerspec

impl Hash for fsid_t

impl Hash for packet_mreq

impl Hash for cpu_set_t

impl Hash for msginfo

impl Hash for sembuf

impl Hash for input_event

impl Hash for input_id

impl Hash for input_mask

impl Hash for ff_replay

impl Hash for ff_trigger

impl Hash for ff_envelope

impl Hash for ff_effect

impl Hash for Elf32_Ehdr

impl Hash for Elf64_Ehdr

impl Hash for Elf32_Sym

impl Hash for Elf64_Sym

impl Hash for Elf32_Phdr

impl Hash for Elf64_Phdr

impl Hash for Elf32_Shdr

impl Hash for Elf64_Shdr

impl Hash for ucred

impl Hash for mntent

impl Hash for genlmsghdr

impl Hash for in6_pktinfo

impl Hash for sockaddr_vm

impl Hash for regmatch_t

impl Hash for can_filter

impl Hash for sock_filter

impl Hash for sock_fprog

impl Hash for nlmsghdr

impl Hash for nlmsgerr

impl Hash for nlattr

impl Hash for sockaddr_nl

impl Hash for dirent

impl Hash for dirent64

impl Hash for af_alg_iv

impl Hash for mq_attr

impl Hash for statx

impl Hash for aiocb

impl Hash for __timeval

impl Hash for glob64_t

impl Hash for msghdr

impl Hash for cmsghdr

impl Hash for termios

impl Hash for mallinfo

impl Hash for mallinfo2

impl Hash for nl_pktinfo

impl Hash for nl_mmap_req

impl Hash for nl_mmap_hdr

impl Hash for rtentry

impl Hash for timex

impl Hash for ntptimeval

impl Hash for regex_t

impl Hash for Elf64_Chdr

impl Hash for Elf32_Chdr

impl Hash for seminfo

impl Hash for utmpx

impl Hash for sigset_t

impl Hash for sysinfo

impl Hash for msqid_ds

impl Hash for semid_ds

impl Hash for sigaction

impl Hash for statfs

impl Hash for flock

impl Hash for flock64

impl Hash for siginfo_t

impl Hash for stack_t

impl Hash for stat

impl Hash for stat64

impl Hash for statfs64

impl Hash for statvfs64

impl Hash for user

impl Hash for mcontext_t

impl Hash for ipc_perm

impl Hash for shmid_ds

impl Hash for ucontext_t

impl Hash for statvfs

impl Hash for sem_t

impl Hash for termios2

impl Hash for open_how

impl Hash for in6_addr

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> Hash for Nested<T>

impl Hash for Dir

impl<'d> Hash for Iter<'d>

impl Hash for OwningIter

impl Hash for Entry

impl Hash for Type

impl Hash for AtFlags

impl Hash for OFlag

impl Hash for RenameFlags

impl Hash for SealFlag

impl Hash for FdFlag

impl<'a> Hash for FcntlArg<'a>

impl Hash for FlockArg

impl Hash for MsFlags

impl Hash for MntFlags

impl Hash for MQ_OFlag

impl Hash for FdFlag

impl Hash for MqAttr

impl Hash for PollFd

impl Hash for PollFlags

impl Hash for PtyMaster

impl Hash for CloneFlags

impl Hash for CpuSet

impl Hash for LioOpcode

impl Hash for LioMode

impl Hash for EpollFlags

impl Hash for EpollOp

impl Hash for EpollEvent

impl Hash for EfdFlags

impl Hash for ProtFlags

impl Hash for MapFlags

impl Hash for MRemapFlags

impl Hash for MmapAdvise

impl Hash for MsFlags

impl Hash for Persona

impl Hash for Request

impl Hash for Event

impl Hash for Options

impl Hash for QuotaType

impl Hash for QuotaFmt

impl Hash for Dqblk

impl Hash for RebootMode

impl Hash for Resource

impl Hash for FdSet

impl Hash for Signal

impl Hash for SaFlags

impl Hash for SigmaskHow

impl Hash for SigSet

impl Hash for SigHandler

impl Hash for SigAction

impl Hash for SigevNotify

impl Hash for SigEvent

impl Hash for SfdFlags

impl Hash for SignalFd

impl Hash for InetAddr

impl Hash for IpAddr

impl Hash for Ipv4Addr

impl Hash for Ipv6Addr

impl Hash for UnixAddr

impl Hash for SockAddr

impl Hash for NetlinkAddr

impl Hash for AlgAddr

impl Hash for LinkAddr

impl Hash for VsockAddr

impl Hash for ReuseAddr

impl Hash for ReusePort

impl Hash for TcpNoDelay

impl Hash for Linger

impl Hash for IpFreebind

impl Hash for SendTimeout

impl Hash for Broadcast

impl Hash for OobInline

impl Hash for SocketError

impl Hash for KeepAlive

impl Hash for TcpKeepIdle

impl Hash for TcpMaxSeg

impl Hash for TcpRepair

impl Hash for RcvBuf

impl Hash for SndBuf

impl Hash for RcvBufForce

impl Hash for SndBufForce

impl Hash for SockType

impl Hash for AcceptConn

impl Hash for OriginalDst

impl Hash for Mark

impl Hash for PassCred

impl Hash for RxqOvfl

impl Hash for Ipv6V6Only

impl Hash for Ipv4RecvErr

impl Hash for Ipv6RecvErr

impl Hash for Ipv4Ttl

impl Hash for Ipv6Ttl

impl Hash for SockFlag

impl Hash for MsgFlags

impl Hash for Shutdown

impl Hash for SFlag

impl Hash for Mode

impl Hash for FsFlags

impl Hash for Statvfs

impl Hash for SysInfo

impl Hash for BaudRate

impl Hash for SetArg

impl Hash for FlushArg

impl Hash for FlowArg

impl Hash for InputFlags

impl Hash for OutputFlags

impl Hash for LocalFlags

impl Hash for TimeSpec

impl Hash for TimeVal

impl Hash for RemoteIoVec

impl<T: Hash> Hash for IoVec<T>

impl Hash for UtsName

impl Hash for WaitPidFlag

impl Hash for WaitStatus

impl Hash for InitFlags

impl Hash for ClockId

impl Hash for TimerFlags

impl Hash for ClockId

impl Hash for UContext

impl Hash for Uid

impl Hash for Gid

impl Hash for Pid

impl Hash for PathconfVar

impl Hash for SysconfVar

impl Hash for AccessFlags

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 RawOsStr

impl Hash for RawOsString

impl Hash for Time

impl<Ix: Hash> Hash for EdgeIndex<Ix> where
    Ix: IndexType

impl<Ix: Hash> Hash for NodeIndex<Ix>

impl<Ix: Hash> Hash for EdgeIndex<Ix>

impl Hash for Direction

impl Hash for Ident

impl Hash for Handle

impl Hash for Version

impl Hash for VersionReq

impl Hash for Comparator

impl Hash for Op

impl Hash for Prerelease

impl Hash for Number

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

impl Hash for ColorLevel

impl Hash for Member

impl Hash for Index

impl Hash for Lifetime

impl Hash for Size

impl Hash for CDataModel

impl Hash for Vendor

impl Hash for Environment

impl Hash for Endianness

impl Hash for Triple

impl Hash for Platform

impl Hash for Triple

impl Hash for Duration

impl Hash for Timespec

impl Hash for Tm

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