@@ -183,6 +183,7 @@ mod sip;
183
183
/// [impl]: ../../std/primitive.str.html#impl-Hash-for-str
184
184
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
185
185
#[ rustc_diagnostic_item = "Hash" ]
186
+ #[ const_trait]
186
187
pub trait Hash {
187
188
/// Feeds this value into the given [`Hasher`].
188
189
///
@@ -234,12 +235,19 @@ pub trait Hash {
234
235
/// [`hash`]: Hash::hash
235
236
/// [`hash_slice`]: Hash::hash_slice
236
237
#[ 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 )
238
239
where
239
240
Self : Sized ,
240
241
{
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 ;
243
251
}
244
252
}
245
253
}
@@ -313,6 +321,7 @@ pub use macros::Hash;
313
321
/// [`write_u8`]: Hasher::write_u8
314
322
/// [`write_u32`]: Hasher::write_u32
315
323
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
324
+ #[ const_trait]
316
325
pub trait Hasher {
317
326
/// Returns the hash value for the values written so far.
318
327
///
@@ -558,7 +567,8 @@ pub trait Hasher {
558
567
}
559
568
560
569
#[ 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 {
562
572
fn finish ( & self ) -> u64 {
563
573
( * * self ) . finish ( )
564
574
}
@@ -806,14 +816,15 @@ mod impls {
806
816
macro_rules! impl_write {
807
817
( $( ( $ty: ident, $meth: ident) , ) * ) => { $(
808
818
#[ 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 {
810
821
#[ inline]
811
- fn hash<H : Hasher >( & self , state: & mut H ) {
822
+ fn hash<H : ~ const Hasher >( & self , state: & mut H ) {
812
823
state. $meth( * self )
813
824
}
814
825
815
826
#[ inline]
816
- fn hash_slice<H : Hasher >( data: & [ $ty] , state: & mut H ) {
827
+ fn hash_slice<H : ~ const Hasher >( data: & [ $ty] , state: & mut H ) {
817
828
let newlen = data. len( ) * mem:: size_of:: <$ty>( ) ;
818
829
let ptr = data. as_ptr( ) as * const u8 ;
819
830
// SAFETY: `ptr` is valid and aligned, as this macro is only used
@@ -842,54 +853,60 @@ mod impls {
842
853
}
843
854
844
855
#[ 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 {
846
858
#[ inline]
847
- fn hash < H : Hasher > ( & self , state : & mut H ) {
859
+ fn hash < H : ~ const Hasher > ( & self , state : & mut H ) {
848
860
state. write_u8 ( * self as u8 )
849
861
}
850
862
}
851
863
852
864
#[ 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 {
854
867
#[ inline]
855
- fn hash < H : Hasher > ( & self , state : & mut H ) {
868
+ fn hash < H : ~ const Hasher > ( & self , state : & mut H ) {
856
869
state. write_u32 ( * self as u32 )
857
870
}
858
871
}
859
872
860
873
#[ 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 {
862
876
#[ inline]
863
- fn hash < H : Hasher > ( & self , state : & mut H ) {
877
+ fn hash < H : ~ const Hasher > ( & self , state : & mut H ) {
864
878
state. write_str ( self ) ;
865
879
}
866
880
}
867
881
868
882
#[ 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 ! {
870
885
#[ inline]
871
- fn hash < H : Hasher > ( & self , _: & mut H ) {
886
+ fn hash < H : ~ const Hasher > ( & self , _: & mut H ) {
872
887
* self
873
888
}
874
889
}
875
890
876
891
macro_rules! impl_hash_tuple {
877
892
( ) => (
878
893
#[ 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 ( ) {
880
896
#[ inline]
881
- fn hash<H : Hasher >( & self , _state: & mut H ) { }
897
+ fn hash<H : ~ const Hasher >( & self , _state: & mut H ) { }
882
898
}
883
899
) ;
884
900
885
901
( $( $name: ident) +) => (
886
902
maybe_tuple_doc! {
887
903
$( $name) + @
888
904
#[ 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 {
890
907
#[ allow( non_snake_case) ]
891
908
#[ inline]
892
- fn hash<S : Hasher >( & self , state: & mut S ) {
909
+ fn hash<S : ~ const Hasher >( & self , state: & mut S ) {
893
910
let ( $( ref $name, ) +) = * self ;
894
911
$( $name. hash( state) ; ) +
895
912
}
@@ -932,24 +949,27 @@ mod impls {
932
949
impl_hash_tuple ! { T B C D E F G H I J K L }
933
950
934
951
#[ 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 ] {
936
954
#[ inline]
937
- fn hash < H : Hasher > ( & self , state : & mut H ) {
955
+ fn hash < H : ~ const Hasher > ( & self , state : & mut H ) {
938
956
state. write_length_prefix ( self . len ( ) ) ;
939
957
Hash :: hash_slice ( self , state)
940
958
}
941
959
}
942
960
943
961
#[ 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 {
945
964
#[ inline]
946
- fn hash < H : Hasher > ( & self , state : & mut H ) {
965
+ fn hash < H : ~ const Hasher > ( & self , state : & mut H ) {
947
966
( * * self ) . hash ( state) ;
948
967
}
949
968
}
950
969
951
970
#[ 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 {
953
973
#[ inline]
954
974
fn hash < H : Hasher > ( & self , state : & mut H ) {
955
975
( * * self ) . hash ( state) ;
0 commit comments