Skip to content

Commit d1ce7ff

Browse files
Document unsafety in src/libcore/hash/mod.rs
1 parent 37514de commit d1ce7ff

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/libcore/hash/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@
7979
//! }
8080
//! ```
8181
82-
// ignore-tidy-undocumented-unsafe
83-
8482
#![stable(feature = "rust1", since = "1.0.0")]
8583

8684
use crate::fmt;
@@ -572,6 +570,9 @@ mod impls {
572570
fn hash_slice<H: Hasher>(data: &[$ty], state: &mut H) {
573571
let newlen = data.len() * mem::size_of::<$ty>();
574572
let ptr = data.as_ptr() as *const u8;
573+
// SAFETY: `ptr` is valid and aligned, the new slice only spans
574+
// across `data` and is never mutated, and its total size is the
575+
// same as the original `data` so it can't be over `isize::MAX`.
575576
state.write(unsafe { slice::from_raw_parts(ptr, newlen) })
576577
}
577578
}
@@ -691,6 +692,8 @@ mod impls {
691692
state.write_usize(*self as *const () as usize);
692693
} else {
693694
// Fat pointer
695+
// SAFETY: we are accessing the memory occupied by `self`
696+
// which is guaranteed to be valid.
694697
let (a, b) = unsafe { *(self as *const Self as *const (usize, usize)) };
695698
state.write_usize(a);
696699
state.write_usize(b);
@@ -706,6 +709,8 @@ mod impls {
706709
state.write_usize(*self as *const () as usize);
707710
} else {
708711
// Fat pointer
712+
// SAFETY: we are accessing the memory occupied by `self`
713+
// which is guaranteed to be valid.
709714
let (a, b) = unsafe { *(self as *const Self as *const (usize, usize)) };
710715
state.write_usize(a);
711716
state.write_usize(b);

0 commit comments

Comments
 (0)