Skip to content

Commit ca2fae8

Browse files
committed
Elaborate on SAFETY comments
1 parent e0140ff commit ca2fae8

File tree

4 files changed

+88
-77
lines changed

4 files changed

+88
-77
lines changed

src/libcore/cell.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,10 @@ impl<T> Cell<T> {
366366
if ptr::eq(self, other) {
367367
return;
368368
}
369-
// SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
369+
// SAFETY: This can be risky if called from separate threads, but `Cell`
370+
// is `!Sync` so this won't happen. This also won't invalidate any
371+
// pointers since `Cell` makes sure nothing else will be pointing into
372+
// either of these `Cell`s.
370373
unsafe {
371374
ptr::swap(self.value.get(), other.value.get());
372375
}
@@ -386,7 +389,8 @@ impl<T> Cell<T> {
386389
/// ```
387390
#[stable(feature = "move_cell", since = "1.17.0")]
388391
pub fn replace(&self, val: T) -> T {
389-
// SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
392+
// SAFETY: This can cause data races if called from a separate thread,
393+
// but `Cell` is `!Sync` so this won't happen.
390394
mem::replace(unsafe { &mut *self.value.get() }, val)
391395
}
392396

@@ -423,7 +427,8 @@ impl<T: Copy> Cell<T> {
423427
#[inline]
424428
#[stable(feature = "rust1", since = "1.0.0")]
425429
pub fn get(&self) -> T {
426-
// SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
430+
// SAFETY: This can cause data races if called from a separate thread,
431+
// but `Cell` is `!Sync` so this won't happen.
427432
unsafe { *self.value.get() }
428433
}
429434

@@ -492,7 +497,9 @@ impl<T: ?Sized> Cell<T> {
492497
#[inline]
493498
#[stable(feature = "cell_get_mut", since = "1.11.0")]
494499
pub fn get_mut(&mut self) -> &mut T {
495-
// SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
500+
// SAFETY: This can cause data races if called from a separate thread,
501+
// but `Cell` is `!Sync` so this won't happen, and `&mut` guarantees
502+
// unique access.
496503
unsafe { &mut *self.value.get() }
497504
}
498505

@@ -512,7 +519,7 @@ impl<T: ?Sized> Cell<T> {
512519
#[inline]
513520
#[stable(feature = "as_cell", since = "1.37.0")]
514521
pub fn from_mut(t: &mut T) -> &Cell<T> {
515-
// SAFETY: `&mut` ensures unique access
522+
// SAFETY: `&mut` ensures unique access.
516523
unsafe { &*(t as *mut T as *const Cell<T>) }
517524
}
518525
}
@@ -556,7 +563,7 @@ impl<T> Cell<[T]> {
556563
/// ```
557564
#[stable(feature = "as_cell", since = "1.37.0")]
558565
pub fn as_slice_of_cells(&self) -> &[Cell<T>] {
559-
// SAFETY: `Cell<T>` has the same memory layout as `T`
566+
// SAFETY: `Cell<T>` has the same memory layout as `T`.
560567
unsafe { &*(self as *const Cell<[T]> as *const [Cell<T>]) }
561568
}
562569
}
@@ -821,7 +828,7 @@ impl<T: ?Sized> RefCell<T> {
821828
pub fn try_borrow(&self) -> Result<Ref<'_, T>, BorrowError> {
822829
match BorrowRef::new(&self.borrow) {
823830
// SAFETY: `BorrowRef` ensures that there is only immutable access
824-
// to the value while borrowed
831+
// to the value while borrowed.
825832
Some(b) => Ok(Ref { value: unsafe { &*self.value.get() }, borrow: b }),
826833
None => Err(BorrowError { _private: () }),
827834
}
@@ -897,7 +904,7 @@ impl<T: ?Sized> RefCell<T> {
897904
#[inline]
898905
pub fn try_borrow_mut(&self) -> Result<RefMut<'_, T>, BorrowMutError> {
899906
match BorrowRefMut::new(&self.borrow) {
900-
// SAFETY: `BorrowRef` guarantees unique access
907+
// SAFETY: `BorrowRef` guarantees unique access.
901908
Some(b) => Ok(RefMut { value: unsafe { &mut *self.value.get() }, borrow: b }),
902909
None => Err(BorrowMutError { _private: () }),
903910
}
@@ -947,7 +954,7 @@ impl<T: ?Sized> RefCell<T> {
947954
#[inline]
948955
#[stable(feature = "cell_get_mut", since = "1.11.0")]
949956
pub fn get_mut(&mut self) -> &mut T {
950-
// SAFETY: `&mut` guarantees unique access
957+
// SAFETY: `&mut` guarantees unique access.
951958
unsafe { &mut *self.value.get() }
952959
}
953960

src/libcore/str/lossy.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl Utf8Lossy {
1515
}
1616

1717
pub fn from_bytes(bytes: &[u8]) -> &Utf8Lossy {
18-
// SAFETY: both use the same memory layout, and UTF-8 correctness isn't required
18+
// SAFETY: Both use the same memory layout, and UTF-8 correctness isn't required.
1919
unsafe { mem::transmute(bytes) }
2020
}
2121

@@ -59,7 +59,8 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {
5959
while i < self.source.len() {
6060
let i_ = i;
6161

62-
// SAFETY: 0 <= i < self.source.len()
62+
// SAFETY: `i` starts at `0`, is less than `self.source.len()`, and
63+
// only increases, so `0 <= i < self.source.len()`.
6364
let byte = unsafe { *self.source.get_unchecked(i) };
6465
i += 1;
6566

@@ -69,7 +70,7 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {
6970

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

133134
let r = Utf8LossyChunk {
134-
// SAFETY: we have checked that the entire source is valid UTF-8
135+
// SAFETY: We have checked that the entire source is valid UTF-8.
135136
valid: unsafe { core_str::from_utf8_unchecked(self.source) },
136137
broken: &[],
137138
};

0 commit comments

Comments
 (0)