Skip to content

Commit 0815531

Browse files
committed
Add a note about why the unsafe is sound
1 parent 7f9883d commit 0815531

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/liballoc/collections/vec_deque.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2018,13 +2018,23 @@ impl<T> VecDeque<T> {
20182018
}
20192019
}
20202020

2021+
// Safety: the following two methods require that the rotation amount
2022+
// be less than half the length of the deque.
2023+
//
2024+
// `wrap_copy` requres that `min(x, cap() - x) + copy_len <= cap()`,
2025+
// but than `min` is never more than half the capacity, regardless of x,
2026+
// so it's sound to call here because we're calling with something
2027+
// less than half the length, which is never above half the capacity.
2028+
20212029
unsafe fn rotate_left_inner(&mut self, mid: usize) {
2030+
debug_assert!(mid * 2 <= self.len());
20222031
self.wrap_copy(self.head, self.tail, mid);
20232032
self.head = self.wrap_add(self.head, mid);
20242033
self.tail = self.wrap_add(self.tail, mid);
20252034
}
20262035

20272036
unsafe fn rotate_right_inner(&mut self, k: usize) {
2037+
debug_assert!(k * 2 <= self.len());
20282038
self.head = self.wrap_sub(self.head, k);
20292039
self.tail = self.wrap_sub(self.tail, k);
20302040
self.wrap_copy(self.tail, self.head, k);

0 commit comments

Comments
 (0)