Skip to content

Commit 267aebc

Browse files
committedNov 9, 2019
rust-lang#66219 documented unsafe in core::cell
1 parent 26ef195 commit 267aebc

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed
 

‎src/libcore/cell.rs

+10-2
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;
@@ -369,6 +367,7 @@ impl<T> Cell<T> {
369367
if ptr::eq(self, other) {
370368
return;
371369
}
370+
// SAFETY: Cell is not Sync
372371
unsafe {
373372
ptr::swap(self.value.get(), other.value.get());
374373
}
@@ -388,6 +387,7 @@ impl<T> Cell<T> {
388387
/// ```
389388
#[stable(feature = "move_cell", since = "1.17.0")]
390389
pub fn replace(&self, val: T) -> T {
390+
// SAFETY: Cell is not Sync
391391
mem::replace(unsafe { &mut *self.value.get() }, val)
392392
}
393393

@@ -424,6 +424,7 @@ impl<T:Copy> Cell<T> {
424424
#[inline]
425425
#[stable(feature = "rust1", since = "1.0.0")]
426426
pub fn get(&self) -> T {
427+
// SAFETY: Cell is not Sync
427428
unsafe{ *self.value.get() }
428429
}
429430

@@ -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: &mut ensures unique access
494496
unsafe {
495497
&mut *self.value.get()
496498
}
@@ -512,6 +514,7 @@ impl<T: ?Sized> Cell<T> {
512514
#[inline]
513515
#[stable(feature = "as_cell", since = "1.37.0")]
514516
pub fn from_mut(t: &mut T) -> &Cell<T> {
517+
// SAFETY: &mut ensures unique access
515518
unsafe {
516519
&*(t as *mut T as *const Cell<T>)
517520
}
@@ -557,6 +560,7 @@ impl<T> Cell<[T]> {
557560
/// ```
558561
#[stable(feature = "as_cell", since = "1.37.0")]
559562
pub fn as_slice_of_cells(&self) -> &[Cell<T>] {
563+
// SAFETY: Cell<T> has the same memory layout as T
560564
unsafe {
561565
&*(self as *const Cell<[T]> as *const [Cell<T>])
562566
}
@@ -825,6 +829,8 @@ impl<T: ?Sized> RefCell<T> {
825829
pub fn try_borrow(&self) -> Result<Ref<'_, T>, BorrowError> {
826830
match BorrowRef::new(&self.borrow) {
827831
Some(b) => Ok(Ref {
832+
// SAFETY: BorrowRef ensures that there is only immutable access to the value while
833+
// borrowed
828834
value: unsafe { &*self.value.get() },
829835
borrow: b,
830836
}),
@@ -903,6 +909,7 @@ impl<T: ?Sized> RefCell<T> {
903909
pub fn try_borrow_mut(&self) -> Result<RefMut<'_, T>, BorrowMutError> {
904910
match BorrowRefMut::new(&self.borrow) {
905911
Some(b) => Ok(RefMut {
912+
// SAFETY: BorrowRef gurantees unique access
906913
value: unsafe { &mut *self.value.get() },
907914
borrow: b,
908915
}),
@@ -954,6 +961,7 @@ impl<T: ?Sized> RefCell<T> {
954961
#[inline]
955962
#[stable(feature = "cell_get_mut", since = "1.11.0")]
956963
pub fn get_mut(&mut self) -> &mut T {
964+
// SAFETY: &mut guarantees unique access
957965
unsafe {
958966
&mut *self.value.get()
959967
}

‎src/libcore/str/pattern.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
352352
#[inline]
353353
fn next_back(&mut self) -> SearchStep {
354354
let old_finger = self.finger_back;
355-
// 1. self.finger and self.old_finger are kept on unicode boundaries (this is invariant)
355+
// 1. self.finger and self.finger_back are kept on unicode boundaries (this is invariant)
356356
// 2. self.finger >= 0 since it starts at 0 and only increases
357357
// 3. self.finger < self.finger_back because otherwise the char iter would return
358358
// SearchStep::Done

0 commit comments

Comments
 (0)
Please sign in to comment.