Skip to content

Commit c40d63b

Browse files
Add invariant to VecDeque::pop_* that len < cap if pop successful
Similar to rust-lang#114370 for VecDeque instead of Vec. It now uses `core::hint::assert_unchecked`.
1 parent 0eefa94 commit c40d63b

File tree

1 file changed

+8
-2
lines changed
  • alloc/src/collections/vec_deque

1 file changed

+8
-2
lines changed

alloc/src/collections/vec_deque/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,10 @@ impl<T, A: Allocator> VecDeque<T, A> {
16141614
let old_head = self.head;
16151615
self.head = self.to_physical_idx(1);
16161616
self.len -= 1;
1617-
Some(unsafe { self.buffer_read(old_head) })
1617+
unsafe {
1618+
core::hint::assert_unchecked(self.len < self.capacity());
1619+
Some(self.buffer_read(old_head))
1620+
}
16181621
}
16191622
}
16201623

@@ -1638,7 +1641,10 @@ impl<T, A: Allocator> VecDeque<T, A> {
16381641
None
16391642
} else {
16401643
self.len -= 1;
1641-
Some(unsafe { self.buffer_read(self.to_physical_idx(self.len)) })
1644+
unsafe {
1645+
core::hint::assert_unchecked(self.len < self.capacity());
1646+
Some(self.buffer_read(self.to_physical_idx(self.len)))
1647+
}
16421648
}
16431649
}
16441650

0 commit comments

Comments
 (0)