Skip to content

Commit 18c2dcc

Browse files
authored
Rollup merge of rust-lang#123089 - Philippe-Cholet:vecdeque_pop_assume_cap, r=Nilstrieb
Add invariant to VecDeque::pop_* that len < cap if pop successful Similar to rust-lang#114370 for VecDeque instead of Vec. I initially come from rust-itertools/itertools#899 where we noticed that `pop_front;push_back;` was slower than expected so `@scottmcm` suggested I file an issue which lead to https://internals.rust-lang.org/t/vecdeque-pop-front-push-back/20483 where **kornel** mentionned rust-lang#114334 (fixed by rust-lang#114370). This is my first time with codegen tests, I based the test on what was done for Vec.
2 parents 07f863d + 8604cb6 commit 18c2dcc

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)