Skip to content

Commit e28287b

Browse files
committed
The unsafety in iter.rs is already documented wonderfully
1 parent 34f7fcb commit e28287b

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/libcore/array/iter.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ where
5151
/// iterator (either via `IntoIterator` for arrays or via another way).
5252
#[unstable(feature = "array_value_iter", issue = "65798")]
5353
pub fn new(array: [T; N]) -> Self {
54-
// The transmute here is actually safe. The docs of `MaybeUninit`
54+
// SAFETY: The transmute here is actually safe. The docs of `MaybeUninit`
5555
// promise:
5656
//
5757
// > `MaybeUninit<T>` is guaranteed to have the same size and alignment
@@ -84,10 +84,10 @@ where
8484
/// Returns an immutable slice of all elements that have not been yielded
8585
/// yet.
8686
fn as_slice(&self) -> &[T] {
87-
// This transmute is safe. As mentioned in `new`, `MaybeUninit` retains
87+
let slice = &self.data[self.alive.clone()];
88+
// SAFETY: This transmute is safe. As mentioned in `new`, `MaybeUninit` retains
8889
// the size and alignment of `T`. Furthermore, we know that all
8990
// elements within `alive` are properly initialized.
90-
let slice = &self.data[self.alive.clone()];
9191
unsafe {
9292
mem::transmute::<&[MaybeUninit<T>], &[T]>(slice)
9393
}
@@ -117,7 +117,8 @@ where
117117
let idx = self.alive.start;
118118
self.alive.start += 1;
119119

120-
// Read the element from the array. This is safe: `idx` is an index
120+
// Read the element from the array.
121+
// SAFETY: This is safe: `idx` is an index
121122
// into the "alive" region of the array. Reading this element means
122123
// that `data[idx]` is regarded as dead now (i.e. do not touch). As
123124
// `idx` was the start of the alive-zone, the alive zone is now
@@ -163,7 +164,8 @@ where
163164
// + 1]`.
164165
self.alive.end -= 1;
165166

166-
// Read the element from the array. This is safe: `alive.end` is an
167+
// Read the element from the array.
168+
// SAFETY: This is safe: `alive.end` is an
167169
// index into the "alive" region of the array. Compare the previous
168170
// comment that states that the alive region is
169171
// `data[alive.start..alive.end + 1]`. Reading this element means that
@@ -226,6 +228,7 @@ where
226228
[T; N]: LengthAtMost32,
227229
{
228230
fn clone(&self) -> Self {
231+
// SAFETY: each point of unsafety is documented inside the unsafe block
229232
unsafe {
230233
// This creates a new uninitialized array. Note that the `assume_init`
231234
// refers to the array, not the individual elements. And it is Ok if

0 commit comments

Comments
 (0)