Skip to content

Commit 0c8e275

Browse files
committed
more safety comments fixed or broken up
1 parent a9b1419 commit 0c8e275

4 files changed

Lines changed: 24 additions & 28 deletions

File tree

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,11 @@ impl<K, V> LeafNode<K, V> {
8686
/// Creates a new boxed `LeafNode`.
8787
fn new<A: Allocator + Clone>(alloc: A) -> Box<Self, A> {
8888
let mut leaf = Box::new_uninit_in(alloc);
89-
// SAFETY: Untriaged.
90-
unsafe {
91-
// SAFETY: `leaf` points to a `LeafNode`
92-
LeafNode::init(leaf.as_mut_ptr());
93-
// SAFETY: `leaf` was just initialized
94-
leaf.assume_init()
95-
}
89+
90+
// SAFETY: `leaf` points to a `LeafNode`.
91+
unsafe { LeafNode::init(leaf.as_mut_ptr()) };
92+
// SAFETY: `leaf` was just initialized.
93+
unsafe { leaf.assume_init() }
9694
}
9795
}
9896

@@ -121,13 +119,11 @@ impl<K, V> InternalNode<K, V> {
121119
/// such an edge.
122120
unsafe fn new<A: Allocator + Clone>(alloc: A) -> Box<Self, A> {
123121
let mut node = Box::<Self, _>::new_uninit_in(alloc);
124-
// SAFETY: Untriaged.
125-
unsafe {
126-
// SAFETY: argument points to the `node.data` `LeafNode`
127-
LeafNode::init(&raw mut (*node.as_mut_ptr()).data);
128-
// SAFETY: `node.data` was just initialized and `node.edges` is MaybeUninit.
129-
node.assume_init()
130-
}
122+
123+
// SAFETY: argument points to the `node.data` `LeafNode`.
124+
unsafe { LeafNode::init(&raw mut (*node.as_mut_ptr()).data) };
125+
// SAFETY: `node.data` was just initialized and `node.edges` is MaybeUninit.
126+
unsafe { node.assume_init() }
131127
}
132128
}
133129

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,9 +1324,10 @@ impl<T, A: Allocator> VecDeque<T, A> {
13241324
// [. . . . . . . . o o o o o o o . ]
13251325
// H L
13261326
// [o o o o o o o . ]
1327-
// SAFETY: Untriaged.
1327+
//
1328+
// SAFETY: `self.head >= target_cap >= self.len`, therefore these accesses
1329+
// do not overlap.
13281330
unsafe {
1329-
// nonoverlapping because `self.head >= target_cap >= self.len`.
13301331
self.copy_nonoverlapping(self.head, WrappedIndex::zero(), self.len);
13311332
}
13321333
self.head = WrappedIndex::zero();
@@ -1423,10 +1424,9 @@ impl<T, A: Allocator> VecDeque<T, A> {
14231424
// There's enough spare capacity to copy the tail to the back (because `tail_len < self.capacity() - target_cap`),
14241425
// and copying the tail should be cheaper than copying the head (because `tail_len <= head_len`).
14251426

1426-
// SAFETY: Untriaged.
1427+
// SAFETY: The old tail and the new tail can't overlap because the head slice lies
1428+
// between them. The head slice ends at `target_cap`, so that's where we copy to.
14271429
unsafe {
1428-
// The old tail and the new tail can't overlap because the head slice lies between them. The
1429-
// head slice ends at `target_cap`, so that's where we copy to.
14301430
self.copy_nonoverlapping(
14311431
WrappedIndex::zero(),
14321432
WrappedIndex::from_arbitrary_number(target_cap),

library/alloc/src/io/cursor.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,9 @@ where
238238
let buf_len = bufs.iter().fold(0usize, |a, b| a.saturating_add(b.len()));
239239
let mut pos = reserve_and_pad(pos_mut, vec, buf_len)?;
240240

241-
// Write the buf then progress the vec forward if necessary
242-
// Safety: we have ensured that the capacity is available
241+
// Write the buf then progress the vec forward if necessary.
242+
// SAFETY: We have ensured that the capacity is available
243243
// and that all bytes get written up to the last pos
244-
// SAFETY: Untriaged.
245244
unsafe {
246245
for buf in bufs {
247246
pos = vec_write_all_unchecked(pos, vec, buf);

library/alloc/src/vec/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2695,14 +2695,15 @@ impl<T, A: Allocator> Vec<T, A> {
26952695
let mut first_duplicate_idx: usize = 1;
26962696
let start = self.as_mut_ptr();
26972697
while first_duplicate_idx != len {
2698-
// SAFETY: Untriaged.
2699-
let found_duplicate = unsafe {
2700-
// SAFETY: first_duplicate always in range [1..len)
2698+
let found_duplicate = {
2699+
// SAFETY: first_duplicate always in range [1..len).
27012700
// Note that we start iteration from 1 so we never overflow.
2702-
let prev = start.add(first_duplicate_idx.wrapping_sub(1));
2703-
let current = start.add(first_duplicate_idx);
2701+
let prev = unsafe { start.add(first_duplicate_idx.wrapping_sub(1)) };
2702+
// SAFETY: Untriaged.
2703+
let current = unsafe { start.add(first_duplicate_idx) };
27042704
// We explicitly say in docs that references are reversed.
2705-
same_bucket(&mut *current, &mut *prev)
2705+
// SAFETY: Untriaged.
2706+
unsafe { same_bucket(&mut *current, &mut *prev) }
27062707
};
27072708
if found_duplicate {
27082709
break;

0 commit comments

Comments
 (0)