Commit c2c1910 1 parent 4c053db commit c2c1910 Copy full SHA for c2c1910
File tree 2 files changed +32
-4
lines changed
2 files changed +32
-4
lines changed Original file line number Diff line number Diff line change @@ -665,16 +665,36 @@ mod impls {
665
665
}
666
666
667
667
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
668
- impl < T > Hash for * const T {
668
+ impl < T : ? Sized > Hash for * const T {
669
669
fn hash < H : Hasher > ( & self , state : & mut H ) {
670
- state. write_usize ( * self as usize )
670
+ if mem:: size_of :: < Self > ( ) == mem:: size_of :: < usize > ( ) {
671
+ // Thin pointer
672
+ state. write_usize ( * self as * const ( ) as usize ) ;
673
+ } else {
674
+ // Fat pointer
675
+ let ( a, b) = unsafe {
676
+ * ( self as * const Self as * const ( usize , usize ) )
677
+ } ;
678
+ state. write_usize ( a) ;
679
+ state. write_usize ( b) ;
680
+ }
671
681
}
672
682
}
673
683
674
684
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
675
- impl < T > Hash for * mut T {
685
+ impl < T : ? Sized > Hash for * mut T {
676
686
fn hash < H : Hasher > ( & self , state : & mut H ) {
677
- state. write_usize ( * self as usize )
687
+ if mem:: size_of :: < Self > ( ) == mem:: size_of :: < usize > ( ) {
688
+ // Thin pointer
689
+ state. write_usize ( * self as * const ( ) as usize ) ;
690
+ } else {
691
+ // Fat pointer
692
+ let ( a, b) = unsafe {
693
+ * ( self as * const Self as * const ( usize , usize ) )
694
+ } ;
695
+ state. write_usize ( a) ;
696
+ state. write_usize ( b) ;
697
+ }
678
698
}
679
699
}
680
700
}
Original file line number Diff line number Diff line change @@ -79,6 +79,14 @@ fn test_writer_hasher() {
79
79
80
80
let ptr = 5_usize as * mut i32 ;
81
81
assert_eq ! ( hash( & ptr) , 5 ) ;
82
+
83
+ let cs: & mut [ u8 ] = & mut [ 1 , 2 , 3 ] ;
84
+ let ptr = cs. as_ptr ( ) ;
85
+ let slice_ptr = cs as * const [ u8 ] ;
86
+ assert_eq ! ( hash( & slice_ptr) , hash( & ptr) + cs. len( ) as u64 ) ;
87
+
88
+ let slice_ptr = cs as * mut [ u8 ] ;
89
+ assert_eq ! ( hash( & slice_ptr) , hash( & ptr) + cs. len( ) as u64 ) ;
82
90
}
83
91
84
92
struct Custom { hash : u64 }
You can’t perform that action at this time.
0 commit comments