Skip to content

Commit 95eb76c

Browse files
committed
fixup some safety comments
1 parent 21f070f commit 95eb76c

4 files changed

Lines changed: 17 additions & 14 deletions

File tree

library/alloc/src/collections/binary_heap/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,11 +1530,13 @@ struct Hole<'a, T: 'a> {
15301530
impl<'a, T> Hole<'a, T> {
15311531
/// Creates a new `Hole` at index `pos`.
15321532
///
1533-
/// Unsafe because pos must be within the data slice.
1533+
/// # Safety
1534+
///
1535+
/// `pos` must be within the data slice.
15341536
#[inline]
15351537
unsafe fn new(data: &'a mut [T], pos: usize) -> Self {
15361538
debug_assert!(pos < data.len());
1537-
// SAFETY: pos should be inside the slice
1539+
// SAFETY: Caller ensures pos is inside the slice.
15381540
let elt = unsafe { ptr::read(data.get_unchecked(pos)) };
15391541
Hole { data, elt: ManuallyDrop::new(elt), pos }
15401542
}
@@ -1553,6 +1555,7 @@ impl<'a, T> Hole<'a, T> {
15531555
/// Returns a reference to the element at `index`.
15541556
///
15551557
/// # Safety
1558+
///
15561559
/// `index` must be within the data slice and not equal to the current position.
15571560
#[inline]
15581561
unsafe fn get(&self, index: usize) -> &T {
@@ -1565,6 +1568,7 @@ impl<'a, T> Hole<'a, T> {
15651568
/// Move hole to new location
15661569
///
15671570
/// # Safety
1571+
///
15681572
/// `index` must be within the data slice and not equal to the current position.
15691573
#[inline]
15701574
unsafe fn move_to(&mut self, index: usize) {

library/alloc/src/collections/btree/mem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub(super) fn replace<T, R>(v: &mut T, change: impl FnOnce(T) -> (T, R)) -> R {
2323
}
2424
}
2525
let guard = PanicGuard;
26-
// SAFETY: v is valid for reads.
26+
// SAFETY: v is valid for reads and we write a new value before returning.
2727
let value = unsafe { ptr::read(v) };
2828
let (new_value, ret) = change(value);
2929
// SAFETY: new_value is T and v is valid for writes.

library/alloc/src/collections/vec_deque/mod.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,9 +1893,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
18931893
// are valid ranges into the physical buffer, so
18941894
// it's ok to pass them to `buffer_range` and
18951895
// dereference the result.
1896-
let a = unsafe { &*self.buffer_range(a_range) };
1897-
// SAFETY: As above.
1898-
let b = unsafe { &*self.buffer_range(b_range) };
1896+
let (a, b) = unsafe { (&*self.buffer_range(a_range), &*self.buffer_range(b_range)) };
1897+
18991898
Iter::new(a.iter(), b.iter())
19001899
}
19011900

@@ -1930,13 +1929,13 @@ impl<T, A: Allocator> VecDeque<T, A> {
19301929
R: RangeBounds<usize>,
19311930
{
19321931
let (a_range, b_range) = self.slice_ranges(range, self.len);
1933-
// SAFETY: The ranges returned by `slice_ranges`
1934-
// are valid ranges into the physical buffer, so
1935-
// it's ok to pass them to `buffer_range` and
1936-
// dereference the result.
1937-
let a = unsafe { &mut *self.buffer_range(a_range) };
1938-
// SAFETY: As above.
1939-
let b = unsafe { &mut *self.buffer_range(b_range) };
1932+
let (a, b) =
1933+
// SAFETY: The ranges returned by `slice_ranges`
1934+
// are valid ranges into the physical buffer, so
1935+
// it's ok to pass them to `buffer_range` and
1936+
// dereference the result.
1937+
unsafe { (&mut *self.buffer_range(a_range), &mut *self.buffer_range(b_range)) };
1938+
19401939
IterMut::new(a.iter_mut(), b.iter_mut())
19411940
}
19421941

library/alloc/src/sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4945,7 +4945,7 @@ impl<T: ?Sized, A: Allocator> UniqueArc<T, A> {
49454945
#[cfg(not(no_global_oom_handling))]
49464946
fn into_inner_with_allocator(this: Self) -> (NonNull<ArcInner<T>>, A) {
49474947
let this = mem::ManuallyDrop::new(this);
4948-
// SAFETY: Pointer is valid for reads and won't be double-dropped.
4948+
// SAFETY: Pointer is valid for reads and only read once.
49494949
(this.ptr, unsafe { ptr::read(&this.alloc) })
49504950
}
49514951

0 commit comments

Comments
 (0)