|
51 | 51 | /// iterator (either via `IntoIterator` for arrays or via another way). |
52 | 52 | #[unstable(feature = "array_value_iter", issue = "65798")] |
53 | 53 | 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` |
55 | 55 | // promise: |
56 | 56 | // |
57 | 57 | // > `MaybeUninit<T>` is guaranteed to have the same size and alignment |
@@ -84,10 +84,10 @@ where |
84 | 84 | /// Returns an immutable slice of all elements that have not been yielded |
85 | 85 | /// yet. |
86 | 86 | 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 |
88 | 89 | // the size and alignment of `T`. Furthermore, we know that all |
89 | 90 | // elements within `alive` are properly initialized. |
90 | | - let slice = &self.data[self.alive.clone()]; |
91 | 91 | unsafe { |
92 | 92 | mem::transmute::<&[MaybeUninit<T>], &[T]>(slice) |
93 | 93 | } |
@@ -117,7 +117,8 @@ where |
117 | 117 | let idx = self.alive.start; |
118 | 118 | self.alive.start += 1; |
119 | 119 |
|
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 |
121 | 122 | // into the "alive" region of the array. Reading this element means |
122 | 123 | // that `data[idx]` is regarded as dead now (i.e. do not touch). As |
123 | 124 | // `idx` was the start of the alive-zone, the alive zone is now |
@@ -163,7 +164,8 @@ where |
163 | 164 | // + 1]`. |
164 | 165 | self.alive.end -= 1; |
165 | 166 |
|
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 |
167 | 169 | // index into the "alive" region of the array. Compare the previous |
168 | 170 | // comment that states that the alive region is |
169 | 171 | // `data[alive.start..alive.end + 1]`. Reading this element means that |
@@ -226,6 +228,7 @@ where |
226 | 228 | [T; N]: LengthAtMost32, |
227 | 229 | { |
228 | 230 | fn clone(&self) -> Self { |
| 231 | + // SAFETY: each point of unsafety is documented inside the unsafe block |
229 | 232 | unsafe { |
230 | 233 | // This creates a new uninitialized array. Note that the `assume_init` |
231 | 234 | // refers to the array, not the individual elements. And it is Ok if |
|
0 commit comments