Skip to content

Commit 19fdc6e

Browse files
committed
Document unsafe blocks in core::{cell, str, sync}
1 parent 8a87b94 commit 19fdc6e

File tree

5 files changed

+91
-17
lines changed

5 files changed

+91
-17
lines changed

src/libcore/cell.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,6 @@
187187
//! ```
188188
//!
189189
190-
// ignore-tidy-undocumented-unsafe
191-
192190
#![stable(feature = "rust1", since = "1.0.0")]
193191

194192
use crate::cmp::Ordering;
@@ -368,6 +366,7 @@ impl<T> Cell<T> {
368366
if ptr::eq(self, other) {
369367
return;
370368
}
369+
// SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
371370
unsafe {
372371
ptr::swap(self.value.get(), other.value.get());
373372
}
@@ -387,6 +386,7 @@ impl<T> Cell<T> {
387386
/// ```
388387
#[stable(feature = "move_cell", since = "1.17.0")]
389388
pub fn replace(&self, val: T) -> T {
389+
// SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
390390
mem::replace(unsafe { &mut *self.value.get() }, val)
391391
}
392392

@@ -423,6 +423,7 @@ impl<T: Copy> Cell<T> {
423423
#[inline]
424424
#[stable(feature = "rust1", since = "1.0.0")]
425425
pub fn get(&self) -> T {
426+
// SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
426427
unsafe { *self.value.get() }
427428
}
428429

@@ -491,6 +492,7 @@ impl<T: ?Sized> Cell<T> {
491492
#[inline]
492493
#[stable(feature = "cell_get_mut", since = "1.11.0")]
493494
pub fn get_mut(&mut self) -> &mut T {
495+
// SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
494496
unsafe { &mut *self.value.get() }
495497
}
496498

@@ -510,6 +512,7 @@ impl<T: ?Sized> Cell<T> {
510512
#[inline]
511513
#[stable(feature = "as_cell", since = "1.37.0")]
512514
pub fn from_mut(t: &mut T) -> &Cell<T> {
515+
// SAFETY: `&mut` ensures unique access
513516
unsafe { &*(t as *mut T as *const Cell<T>) }
514517
}
515518
}
@@ -553,6 +556,7 @@ impl<T> Cell<[T]> {
553556
/// ```
554557
#[stable(feature = "as_cell", since = "1.37.0")]
555558
pub fn as_slice_of_cells(&self) -> &[Cell<T>] {
559+
// SAFETY: `Cell<T>` has the same memory layout as `T`
556560
unsafe { &*(self as *const Cell<[T]> as *const [Cell<T>]) }
557561
}
558562
}
@@ -816,6 +820,8 @@ impl<T: ?Sized> RefCell<T> {
816820
#[inline]
817821
pub fn try_borrow(&self) -> Result<Ref<'_, T>, BorrowError> {
818822
match BorrowRef::new(&self.borrow) {
823+
// SAFETY: `BorrowRef` ensures that there is only immutable access
824+
// to the value while borrowed
819825
Some(b) => Ok(Ref { value: unsafe { &*self.value.get() }, borrow: b }),
820826
None => Err(BorrowError { _private: () }),
821827
}
@@ -891,6 +897,7 @@ impl<T: ?Sized> RefCell<T> {
891897
#[inline]
892898
pub fn try_borrow_mut(&self) -> Result<RefMut<'_, T>, BorrowMutError> {
893899
match BorrowRefMut::new(&self.borrow) {
900+
// SAFETY: `BorrowRef` gurantees unique access
894901
Some(b) => Ok(RefMut { value: unsafe { &mut *self.value.get() }, borrow: b }),
895902
None => Err(BorrowMutError { _private: () }),
896903
}
@@ -940,6 +947,7 @@ impl<T: ?Sized> RefCell<T> {
940947
#[inline]
941948
#[stable(feature = "cell_get_mut", since = "1.11.0")]
942949
pub fn get_mut(&mut self) -> &mut T {
950+
// SAFETY: `&mut` guarantees unique access
943951
unsafe { &mut *self.value.get() }
944952
}
945953

src/libcore/str/lossy.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use crate::fmt::{self, Write};
33
use crate::mem;
44
use crate::str as core_str;
55

6-
// ignore-tidy-undocumented-unsafe
7-
86
/// Lossy UTF-8 string.
97
#[unstable(feature = "str_internals", issue = "none")]
108
pub struct Utf8Lossy {
@@ -17,6 +15,7 @@ impl Utf8Lossy {
1715
}
1816

1917
pub fn from_bytes(bytes: &[u8]) -> &Utf8Lossy {
18+
// SAFETY: both use the same memory layout, and UTF-8 correctness isn't required
2019
unsafe { mem::transmute(bytes) }
2120
}
2221

@@ -60,6 +59,7 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {
6059
while i < self.source.len() {
6160
let i_ = i;
6261

62+
// SAFETY: 0 <= i < self.source.len()
6363
let byte = unsafe { *self.source.get_unchecked(i) };
6464
i += 1;
6565

@@ -69,6 +69,7 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {
6969

7070
macro_rules! error {
7171
() => {{
72+
// SAFETY: we have checked up to `i` that source is valid UTF-8
7273
unsafe {
7374
let r = Utf8LossyChunk {
7475
valid: core_str::from_utf8_unchecked(&self.source[0..i_]),
@@ -130,6 +131,7 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {
130131
}
131132

132133
let r = Utf8LossyChunk {
134+
// SAFETY: we have checked that the entire source is valid UTF-8
133135
valid: unsafe { core_str::from_utf8_unchecked(self.source) },
134136
broken: &[],
135137
};

0 commit comments

Comments
 (0)