1#![stable(feature = "rust1", since = "1.0.0")]
85
86#[stable(feature = "rust1", since = "1.0.0")]
87#[allow(deprecated)]
88pub use self::sip::SipHasher;
89#[unstable(feature = "hashmap_internals", issue = "none")]
90#[allow(deprecated)]
91#[doc(hidden)]
92pub use self::sip::SipHasher13;
93use crate::{fmt, marker};
94
95mod sip;
96
97#[stable(feature = "rust1", since = "1.0.0")]
185#[rustc_diagnostic_item = "Hash"]
186pub trait Hash: marker::PointeeSized {
187 #[stable(feature = "rust1", since = "1.0.0")]
199 fn hash<H: Hasher>(&self, state: &mut H);
200
201 #[stable(feature = "hash_slice", since = "1.3.0")]
235 fn hash_slice<H: Hasher>(data: &[Self], state: &mut H)
236 where
237 Self: Sized,
238 {
239 for piece in data {
240 piece.hash(state)
241 }
242 }
243}
244
245pub(crate) mod macros {
247 #[rustc_builtin_macro]
249 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
250 #[allow_internal_unstable(core_intrinsics)]
251 pub macro Hash($item:item) {
252 }
254}
255#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
256#[doc(inline)]
257pub use macros::Hash;
258
259#[stable(feature = "rust1", since = "1.0.0")]
313pub trait Hasher {
314 #[stable(feature = "rust1", since = "1.0.0")]
334 #[must_use]
335 fn finish(&self) -> u64;
336
337 #[stable(feature = "rust1", since = "1.0.0")]
358 fn write(&mut self, bytes: &[u8]);
359
360 #[inline]
362 #[stable(feature = "hasher_write", since = "1.3.0")]
363 fn write_u8(&mut self, i: u8) {
364 self.write(&[i])
365 }
366 #[inline]
368 #[stable(feature = "hasher_write", since = "1.3.0")]
369 fn write_u16(&mut self, i: u16) {
370 self.write(&i.to_ne_bytes())
371 }
372 #[inline]
374 #[stable(feature = "hasher_write", since = "1.3.0")]
375 fn write_u32(&mut self, i: u32) {
376 self.write(&i.to_ne_bytes())
377 }
378 #[inline]
380 #[stable(feature = "hasher_write", since = "1.3.0")]
381 fn write_u64(&mut self, i: u64) {
382 self.write(&i.to_ne_bytes())
383 }
384 #[inline]
386 #[stable(feature = "i128", since = "1.26.0")]
387 fn write_u128(&mut self, i: u128) {
388 self.write(&i.to_ne_bytes())
389 }
390 #[inline]
392 #[stable(feature = "hasher_write", since = "1.3.0")]
393 fn write_usize(&mut self, i: usize) {
394 self.write(&i.to_ne_bytes())
395 }
396
397 #[inline]
399 #[stable(feature = "hasher_write", since = "1.3.0")]
400 fn write_i8(&mut self, i: i8) {
401 self.write_u8(i as u8)
402 }
403 #[inline]
405 #[stable(feature = "hasher_write", since = "1.3.0")]
406 fn write_i16(&mut self, i: i16) {
407 self.write_u16(i as u16)
408 }
409 #[inline]
411 #[stable(feature = "hasher_write", since = "1.3.0")]
412 fn write_i32(&mut self, i: i32) {
413 self.write_u32(i as u32)
414 }
415 #[inline]
417 #[stable(feature = "hasher_write", since = "1.3.0")]
418 fn write_i64(&mut self, i: i64) {
419 self.write_u64(i as u64)
420 }
421 #[inline]
423 #[stable(feature = "i128", since = "1.26.0")]
424 fn write_i128(&mut self, i: i128) {
425 self.write_u128(i as u128)
426 }
427 #[inline]
429 #[stable(feature = "hasher_write", since = "1.3.0")]
430 fn write_isize(&mut self, i: isize) {
431 self.write_usize(i as usize)
432 }
433
434 #[inline]
483 #[unstable(feature = "hasher_prefixfree_extras", issue = "96762")]
484 fn write_length_prefix(&mut self, len: usize) {
485 self.write_usize(len);
486 }
487
488 #[inline]
550 #[unstable(feature = "hasher_prefixfree_extras", issue = "96762")]
551 fn write_str(&mut self, s: &str) {
552 self.write(s.as_bytes());
553 self.write_u8(0xff);
554 }
555}
556
557#[stable(feature = "indirect_hasher_impl", since = "1.22.0")]
558impl<H: Hasher + ?Sized> Hasher for &mut H {
559 fn finish(&self) -> u64 {
560 (**self).finish()
561 }
562 fn write(&mut self, bytes: &[u8]) {
563 (**self).write(bytes)
564 }
565 fn write_u8(&mut self, i: u8) {
566 (**self).write_u8(i)
567 }
568 fn write_u16(&mut self, i: u16) {
569 (**self).write_u16(i)
570 }
571 fn write_u32(&mut self, i: u32) {
572 (**self).write_u32(i)
573 }
574 fn write_u64(&mut self, i: u64) {
575 (**self).write_u64(i)
576 }
577 fn write_u128(&mut self, i: u128) {
578 (**self).write_u128(i)
579 }
580 fn write_usize(&mut self, i: usize) {
581 (**self).write_usize(i)
582 }
583 fn write_i8(&mut self, i: i8) {
584 (**self).write_i8(i)
585 }
586 fn write_i16(&mut self, i: i16) {
587 (**self).write_i16(i)
588 }
589 fn write_i32(&mut self, i: i32) {
590 (**self).write_i32(i)
591 }
592 fn write_i64(&mut self, i: i64) {
593 (**self).write_i64(i)
594 }
595 fn write_i128(&mut self, i: i128) {
596 (**self).write_i128(i)
597 }
598 fn write_isize(&mut self, i: isize) {
599 (**self).write_isize(i)
600 }
601 fn write_length_prefix(&mut self, len: usize) {
602 (**self).write_length_prefix(len)
603 }
604 fn write_str(&mut self, s: &str) {
605 (**self).write_str(s)
606 }
607}
608
609#[stable(since = "1.7.0", feature = "build_hasher")]
637pub trait BuildHasher {
638 #[stable(since = "1.7.0", feature = "build_hasher")]
640 type Hasher: Hasher;
641
642 #[stable(since = "1.7.0", feature = "build_hasher")]
656 fn build_hasher(&self) -> Self::Hasher;
657
658 #[stable(feature = "build_hasher_simple_hash_one", since = "1.71.0")]
694 fn hash_one<T: Hash>(&self, x: T) -> u64
695 where
696 Self: Sized,
697 Self::Hasher: Hasher,
698 {
699 let mut hasher = self.build_hasher();
700 x.hash(&mut hasher);
701 hasher.finish()
702 }
703}
704
705#[stable(since = "1.7.0", feature = "build_hasher")]
751pub struct BuildHasherDefault<H>(marker::PhantomData<fn() -> H>);
752
753impl<H> BuildHasherDefault<H> {
754 #[stable(feature = "build_hasher_default_const_new", since = "1.85.0")]
756 #[rustc_const_stable(feature = "build_hasher_default_const_new", since = "1.85.0")]
757 pub const fn new() -> Self {
758 BuildHasherDefault(marker::PhantomData)
759 }
760}
761
762#[stable(since = "1.9.0", feature = "core_impl_debug")]
763impl<H> fmt::Debug for BuildHasherDefault<H> {
764 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
765 f.debug_struct("BuildHasherDefault").finish()
766 }
767}
768
769#[stable(since = "1.7.0", feature = "build_hasher")]
770impl<H: Default + Hasher> BuildHasher for BuildHasherDefault<H> {
771 type Hasher = H;
772
773 fn build_hasher(&self) -> H {
774 H::default()
775 }
776}
777
778#[stable(since = "1.7.0", feature = "build_hasher")]
779impl<H> Clone for BuildHasherDefault<H> {
780 fn clone(&self) -> BuildHasherDefault<H> {
781 BuildHasherDefault(marker::PhantomData)
782 }
783}
784
785#[stable(since = "1.7.0", feature = "build_hasher")]
786impl<H> Default for BuildHasherDefault<H> {
787 fn default() -> BuildHasherDefault<H> {
788 Self::new()
789 }
790}
791
792#[stable(since = "1.29.0", feature = "build_hasher_eq")]
793impl<H> PartialEq for BuildHasherDefault<H> {
794 fn eq(&self, _other: &BuildHasherDefault<H>) -> bool {
795 true
796 }
797}
798
799#[stable(since = "1.29.0", feature = "build_hasher_eq")]
800impl<H> Eq for BuildHasherDefault<H> {}
801
802mod impls {
803 use super::*;
804 use crate::slice;
805
806 macro_rules! impl_write {
807 ($(($ty:ident, $meth:ident),)*) => {$(
808 #[stable(feature = "rust1", since = "1.0.0")]
809 impl Hash for $ty {
810 #[inline]
811 fn hash<H: Hasher>(&self, state: &mut H) {
812 state.$meth(*self)
813 }
814
815 #[inline]
816 fn hash_slice<H: Hasher>(data: &[$ty], state: &mut H) {
817 let newlen = size_of_val(data);
818 let ptr = data.as_ptr() as *const u8;
819 state.write(unsafe { slice::from_raw_parts(ptr, newlen) })
824 }
825 }
826 )*}
827 }
828
829 impl_write! {
830 (u8, write_u8),
831 (u16, write_u16),
832 (u32, write_u32),
833 (u64, write_u64),
834 (usize, write_usize),
835 (i8, write_i8),
836 (i16, write_i16),
837 (i32, write_i32),
838 (i64, write_i64),
839 (isize, write_isize),
840 (u128, write_u128),
841 (i128, write_i128),
842 }
843
844 #[stable(feature = "rust1", since = "1.0.0")]
845 impl Hash for bool {
846 #[inline]
847 fn hash<H: Hasher>(&self, state: &mut H) {
848 state.write_u8(*self as u8)
849 }
850 }
851
852 #[stable(feature = "rust1", since = "1.0.0")]
853 impl Hash for char {
854 #[inline]
855 fn hash<H: Hasher>(&self, state: &mut H) {
856 state.write_u32(*self as u32)
857 }
858 }
859
860 #[stable(feature = "rust1", since = "1.0.0")]
861 impl Hash for str {
862 #[inline]
863 fn hash<H: Hasher>(&self, state: &mut H) {
864 state.write_str(self);
865 }
866 }
867
868 #[stable(feature = "never_hash", since = "1.29.0")]
869 impl Hash for ! {
870 #[inline]
871 fn hash<H: Hasher>(&self, _: &mut H) {
872 *self
873 }
874 }
875
876 macro_rules! impl_hash_tuple {
877 () => (
878 #[stable(feature = "rust1", since = "1.0.0")]
879 impl Hash for () {
880 #[inline]
881 fn hash<H: Hasher>(&self, _state: &mut H) {}
882 }
883 );
884
885 ( $($name:ident)+) => (
886 maybe_tuple_doc! {
887 $($name)+ @
888 #[stable(feature = "rust1", since = "1.0.0")]
889 impl<$($name: Hash),+> Hash for ($($name,)+) {
890 #[allow(non_snake_case)]
891 #[inline]
892 fn hash<S: Hasher>(&self, state: &mut S) {
893 let ($(ref $name,)+) = *self;
894 $($name.hash(state);)+
895 }
896 }
897 }
898 );
899 }
900
901 macro_rules! maybe_tuple_doc {
902 ($a:ident @ #[$meta:meta] $item:item) => {
903 #[doc(fake_variadic)]
904 #[doc = "This trait is implemented for tuples up to twelve items long."]
905 #[$meta]
906 $item
907 };
908 ($a:ident $($rest_a:ident)+ @ #[$meta:meta] $item:item) => {
909 #[doc(hidden)]
910 #[$meta]
911 $item
912 };
913 }
914
915 impl_hash_tuple! {}
916 impl_hash_tuple! { T }
917 impl_hash_tuple! { T B }
918 impl_hash_tuple! { T B C }
919 impl_hash_tuple! { T B C D }
920 impl_hash_tuple! { T B C D E }
921 impl_hash_tuple! { T B C D E F }
922 impl_hash_tuple! { T B C D E F G }
923 impl_hash_tuple! { T B C D E F G H }
924 impl_hash_tuple! { T B C D E F G H I }
925 impl_hash_tuple! { T B C D E F G H I J }
926 impl_hash_tuple! { T B C D E F G H I J K }
927 impl_hash_tuple! { T B C D E F G H I J K L }
928
929 #[stable(feature = "rust1", since = "1.0.0")]
930 impl<T: Hash> Hash for [T] {
931 #[inline]
932 fn hash<H: Hasher>(&self, state: &mut H) {
933 state.write_length_prefix(self.len());
934 Hash::hash_slice(self, state)
935 }
936 }
937
938 #[stable(feature = "rust1", since = "1.0.0")]
939 impl<T: ?Sized + marker::PointeeSized + Hash> Hash for &T {
940 #[inline]
941 fn hash<H: Hasher>(&self, state: &mut H) {
942 (**self).hash(state);
943 }
944 }
945
946 #[stable(feature = "rust1", since = "1.0.0")]
947 impl<T: ?Sized + marker::PointeeSized + Hash> Hash for &mut T {
948 #[inline]
949 fn hash<H: Hasher>(&self, state: &mut H) {
950 (**self).hash(state);
951 }
952 }
953
954 #[stable(feature = "rust1", since = "1.0.0")]
955 impl<T: ?Sized + marker::PointeeSized> Hash for *const T {
956 #[inline]
957 fn hash<H: Hasher>(&self, state: &mut H) {
958 let (address, metadata) = self.to_raw_parts();
959 state.write_usize(address.addr());
960 metadata.hash(state);
961 }
962 }
963
964 #[stable(feature = "rust1", since = "1.0.0")]
965 impl<T: ?Sized + marker::PointeeSized> Hash for *mut T {
966 #[inline]
967 fn hash<H: Hasher>(&self, state: &mut H) {
968 let (address, metadata) = self.to_raw_parts();
969 state.write_usize(address.addr());
970 metadata.hash(state);
971 }
972 }
973}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4