Skip to content

Commit 1bcf9fa

Browse files
committed
Made Hash and Hasher const_trait
1 parent c2a5c3a commit 1bcf9fa

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

library/core/src/hash/mod.rs

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ mod sip;
183183
/// [impl]: ../../std/primitive.str.html#impl-Hash-for-str
184184
#[stable(feature = "rust1", since = "1.0.0")]
185185
#[rustc_diagnostic_item = "Hash"]
186+
#[const_trait]
186187
pub trait Hash {
187188
/// Feeds this value into the given [`Hasher`].
188189
///
@@ -234,12 +235,19 @@ pub trait Hash {
234235
/// [`hash`]: Hash::hash
235236
/// [`hash_slice`]: Hash::hash_slice
236237
#[stable(feature = "hash_slice", since = "1.3.0")]
237-
fn hash_slice<H: Hasher>(data: &[Self], state: &mut H)
238+
fn hash_slice<H: ~const Hasher>(data: &[Self], state: &mut H)
238239
where
239240
Self: Sized,
240241
{
241-
for piece in data {
242-
piece.hash(state);
242+
//FIXME(const_iter_slice): Revert to for loop
243+
//for piece in data {
244+
// piece.hash(state);
245+
//}
246+
247+
let mut i = 0;
248+
while i < data.len() {
249+
data[i].hash(state);
250+
i += 1;
243251
}
244252
}
245253
}
@@ -313,6 +321,7 @@ pub use macros::Hash;
313321
/// [`write_u8`]: Hasher::write_u8
314322
/// [`write_u32`]: Hasher::write_u32
315323
#[stable(feature = "rust1", since = "1.0.0")]
324+
#[const_trait]
316325
pub trait Hasher {
317326
/// Returns the hash value for the values written so far.
318327
///
@@ -558,7 +567,8 @@ pub trait Hasher {
558567
}
559568

560569
#[stable(feature = "indirect_hasher_impl", since = "1.22.0")]
561-
impl<H: Hasher + ?Sized> Hasher for &mut H {
570+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
571+
impl<H: ~const Hasher + ?Sized> const Hasher for &mut H {
562572
fn finish(&self) -> u64 {
563573
(**self).finish()
564574
}
@@ -806,14 +816,15 @@ mod impls {
806816
macro_rules! impl_write {
807817
($(($ty:ident, $meth:ident),)*) => {$(
808818
#[stable(feature = "rust1", since = "1.0.0")]
809-
impl Hash for $ty {
819+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
820+
impl const Hash for $ty {
810821
#[inline]
811-
fn hash<H: Hasher>(&self, state: &mut H) {
822+
fn hash<H: ~const Hasher>(&self, state: &mut H) {
812823
state.$meth(*self)
813824
}
814825

815826
#[inline]
816-
fn hash_slice<H: Hasher>(data: &[$ty], state: &mut H) {
827+
fn hash_slice<H: ~const Hasher>(data: &[$ty], state: &mut H) {
817828
let newlen = data.len() * mem::size_of::<$ty>();
818829
let ptr = data.as_ptr() as *const u8;
819830
// SAFETY: `ptr` is valid and aligned, as this macro is only used
@@ -842,54 +853,60 @@ mod impls {
842853
}
843854

844855
#[stable(feature = "rust1", since = "1.0.0")]
845-
impl Hash for bool {
856+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
857+
impl const Hash for bool {
846858
#[inline]
847-
fn hash<H: Hasher>(&self, state: &mut H) {
859+
fn hash<H: ~const Hasher>(&self, state: &mut H) {
848860
state.write_u8(*self as u8)
849861
}
850862
}
851863

852864
#[stable(feature = "rust1", since = "1.0.0")]
853-
impl Hash for char {
865+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
866+
impl const Hash for char {
854867
#[inline]
855-
fn hash<H: Hasher>(&self, state: &mut H) {
868+
fn hash<H: ~const Hasher>(&self, state: &mut H) {
856869
state.write_u32(*self as u32)
857870
}
858871
}
859872

860873
#[stable(feature = "rust1", since = "1.0.0")]
861-
impl Hash for str {
874+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
875+
impl const Hash for str {
862876
#[inline]
863-
fn hash<H: Hasher>(&self, state: &mut H) {
877+
fn hash<H: ~const Hasher>(&self, state: &mut H) {
864878
state.write_str(self);
865879
}
866880
}
867881

868882
#[stable(feature = "never_hash", since = "1.29.0")]
869-
impl Hash for ! {
883+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
884+
impl const Hash for ! {
870885
#[inline]
871-
fn hash<H: Hasher>(&self, _: &mut H) {
886+
fn hash<H: ~const Hasher>(&self, _: &mut H) {
872887
*self
873888
}
874889
}
875890

876891
macro_rules! impl_hash_tuple {
877892
() => (
878893
#[stable(feature = "rust1", since = "1.0.0")]
879-
impl Hash for () {
894+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
895+
impl const Hash for () {
880896
#[inline]
881-
fn hash<H: Hasher>(&self, _state: &mut H) {}
897+
fn hash<H: ~const Hasher>(&self, _state: &mut H) {}
882898
}
883899
);
884900

885901
( $($name:ident)+) => (
886902
maybe_tuple_doc! {
887903
$($name)+ @
888904
#[stable(feature = "rust1", since = "1.0.0")]
889-
impl<$($name: Hash),+> Hash for ($($name,)+) where last_type!($($name,)+): ?Sized {
905+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
906+
impl<$($name: ~const Hash),+> const Hash for ($($name,)+) where last_type!($($name,)+): ?Sized {
890907
#[allow(non_snake_case)]
891908
#[inline]
892-
fn hash<S: Hasher>(&self, state: &mut S) {
909+
fn hash<S: ~const Hasher>(&self, state: &mut S) {
893910
let ($(ref $name,)+) = *self;
894911
$($name.hash(state);)+
895912
}
@@ -932,24 +949,27 @@ mod impls {
932949
impl_hash_tuple! { T B C D E F G H I J K L }
933950

934951
#[stable(feature = "rust1", since = "1.0.0")]
935-
impl<T: Hash> Hash for [T] {
952+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
953+
impl<T: ~const Hash> const Hash for [T] {
936954
#[inline]
937-
fn hash<H: Hasher>(&self, state: &mut H) {
955+
fn hash<H: ~const Hasher>(&self, state: &mut H) {
938956
state.write_length_prefix(self.len());
939957
Hash::hash_slice(self, state)
940958
}
941959
}
942960

943961
#[stable(feature = "rust1", since = "1.0.0")]
944-
impl<T: ?Sized + Hash> Hash for &T {
962+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
963+
impl<T: ?Sized + ~const Hash> const Hash for &T {
945964
#[inline]
946-
fn hash<H: Hasher>(&self, state: &mut H) {
965+
fn hash<H: ~const Hasher>(&self, state: &mut H) {
947966
(**self).hash(state);
948967
}
949968
}
950969

951970
#[stable(feature = "rust1", since = "1.0.0")]
952-
impl<T: ?Sized + Hash> Hash for &mut T {
971+
#[rustc_const_unstable(feature = "const_hash", issue = "none")]
972+
impl<T: ?Sized + ~const Hash> const Hash for &mut T {
953973
#[inline]
954974
fn hash<H: Hasher>(&self, state: &mut H) {
955975
(**self).hash(state);

0 commit comments

Comments
 (0)