Skip to content

Commit 36a0974

Browse files
committed
rust-lang#66219 finished documenting libcore!
1 parent ac20308 commit 36a0974

File tree

5 files changed

+100
-18
lines changed

5 files changed

+100
-18
lines changed

src/libcore/iter/adapters/zip.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// ignore-tidy-undocumented-unsafe
2-
31
use crate::cmp;
42

53
use super::super::{Iterator, DoubleEndedIterator, ExactSizeIterator, FusedIterator, TrustedLen};
@@ -165,11 +163,13 @@ impl<A, B> ZipImpl<A, B> for Zip<A, B>
165163
if self.index < self.len {
166164
let i = self.index;
167165
self.index += 1;
166+
// SAFETY: checked that i < min(a.len(), b.len())
168167
unsafe {
169168
Some((self.a.get_unchecked(i), self.b.get_unchecked(i)))
170169
}
171170
} else if A::may_have_side_effect() && self.index < self.a.len() {
172171
// match the base implementation's potential side effects
172+
// SAFETY: checked that index < a.len()
173173
unsafe {
174174
self.a.get_unchecked(self.index);
175175
}
@@ -194,9 +194,11 @@ impl<A, B> ZipImpl<A, B> for Zip<A, B>
194194
let i = self.index;
195195
self.index += 1;
196196
if A::may_have_side_effect() {
197+
// SAFETY: i < end < self.len
197198
unsafe { self.a.get_unchecked(i); }
198199
}
199200
if B::may_have_side_effect() {
201+
// SAFETY: i < end < self.len
200202
unsafe { self.b.get_unchecked(i); }
201203
}
202204
}
@@ -229,6 +231,7 @@ impl<A, B> ZipImpl<A, B> for Zip<A, B>
229231
if self.index < self.len {
230232
self.len -= 1;
231233
let i = self.len;
234+
// SAFETY: i < min(a.len(), b.len())
232235
unsafe {
233236
Some((self.a.get_unchecked(i), self.b.get_unchecked(i)))
234237
}

src/libcore/mem/maybe_uninit.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use crate::intrinsics;
22
use crate::mem::ManuallyDrop;
33

4-
// ignore-tidy-undocumented-unsafe
5-
64
/// A wrapper type to construct uninitialized instances of `T`.
75
///
86
/// # Initialization invariant
@@ -292,6 +290,7 @@ impl<T> MaybeUninit<T> {
292290
#[unstable(feature = "maybe_uninit_uninit_array", issue = "0")]
293291
#[inline(always)]
294292
pub fn uninit_array<const LEN: usize>() -> [Self; LEN] {
293+
// SAFETY: see type-level documentation
295294
unsafe {
296295
MaybeUninit::<[MaybeUninit<T>; LEN]>::uninit().assume_init()
297296
}
@@ -341,6 +340,7 @@ impl<T> MaybeUninit<T> {
341340
#[inline]
342341
pub fn zeroed() -> MaybeUninit<T> {
343342
let mut u = MaybeUninit::<T>::uninit();
343+
// SAFETY: depends on T, see above comment
344344
unsafe {
345345
u.as_mut_ptr().write_bytes(0u8, 1);
346346
}
@@ -354,6 +354,7 @@ impl<T> MaybeUninit<T> {
354354
#[unstable(feature = "maybe_uninit_extra", issue = "63567")]
355355
#[inline(always)]
356356
pub fn write(&mut self, val: T) -> &mut T {
357+
// SAFETY: initializes field, and returns reference to the value
357358
unsafe {
358359
self.value = ManuallyDrop::new(val);
359360
self.get_mut()
@@ -394,6 +395,7 @@ impl<T> MaybeUninit<T> {
394395
#[stable(feature = "maybe_uninit", since = "1.36.0")]
395396
#[inline(always)]
396397
pub fn as_ptr(&self) -> *const T {
398+
// SAFETY: unsafe if uninitialized
397399
unsafe { &*self.value as *const T }
398400
}
399401

@@ -431,6 +433,7 @@ impl<T> MaybeUninit<T> {
431433
#[stable(feature = "maybe_uninit", since = "1.36.0")]
432434
#[inline(always)]
433435
pub fn as_mut_ptr(&mut self) -> *mut T {
436+
// SAFETY: unsafe if uninitialized
434437
unsafe { &mut *self.value as *mut T }
435438
}
436439

src/libcore/slice/memchr.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Original implementation taken from rust-memchr.
22
// Copyright 2015 Andrew Gallant, bluss and Nicolas Koch
33

4-
// ignore-tidy-undocumented-unsafe
5-
64
use crate::cmp;
75
use crate::mem;
86

@@ -63,6 +61,9 @@ pub fn memchr(x: u8, text: &[u8]) -> Option<usize> {
6361

6462
if len >= 2 * usize_bytes {
6563
while offset <= len - 2 * usize_bytes {
64+
// SAFETY: both u and v can be read since
65+
// ptr + offset + usize_bytes <= ptr + len - usize_bytes < ptr + len
66+
// means the pointers are in bounds
6667
unsafe {
6768
let u = *(ptr.add(offset) as *const usize);
6869
let v = *(ptr.add(offset + usize_bytes) as *const usize);
@@ -95,7 +96,7 @@ pub fn memrchr(x: u8, text: &[u8]) -> Option<usize> {
9596
type Chunk = usize;
9697

9798
let (min_aligned_offset, max_aligned_offset) = {
98-
// We call this just to obtain the length of the prefix and suffix.
99+
// SAFETY: We call this just to obtain the length of the prefix and suffix.
99100
// In the middle we always process two chunks at once.
100101
let (prefix, _, suffix) = unsafe { text.align_to::<(Chunk, Chunk)>() };
101102
(prefix.len(), len - suffix.len())
@@ -113,6 +114,8 @@ pub fn memrchr(x: u8, text: &[u8]) -> Option<usize> {
113114
let chunk_bytes = mem::size_of::<Chunk>();
114115

115116
while offset > min_aligned_offset {
117+
// SAFETY: since offset is always aligned, offset > min_aligned_offset means
118+
// that offset - 2 * chunk_bytes is within bounds.
116119
unsafe {
117120
let u = *(ptr.offset(offset as isize - 2 * chunk_bytes as isize) as *const Chunk);
118121
let v = *(ptr.offset(offset as isize - chunk_bytes as isize) as *const Chunk);

0 commit comments

Comments
 (0)