Avoid overflow in VecDeque::with_capacity_in()
.
This commit is contained in:
parent
5e02151318
commit
4a37b9cbff
1 changed files with 1 additions and 1 deletions
|
@ -543,9 +543,9 @@ impl<T, A: Allocator> VecDeque<T, A> {
|
||||||
/// ```
|
/// ```
|
||||||
#[unstable(feature = "allocator_api", issue = "32838")]
|
#[unstable(feature = "allocator_api", issue = "32838")]
|
||||||
pub fn with_capacity_in(capacity: usize, alloc: A) -> VecDeque<T, A> {
|
pub fn with_capacity_in(capacity: usize, alloc: A) -> VecDeque<T, A> {
|
||||||
|
assert!(capacity < 1_usize << usize::BITS - 1, "capacity overflow");
|
||||||
// +1 since the ringbuffer always leaves one space empty
|
// +1 since the ringbuffer always leaves one space empty
|
||||||
let cap = cmp::max(capacity + 1, MINIMUM_CAPACITY + 1).next_power_of_two();
|
let cap = cmp::max(capacity + 1, MINIMUM_CAPACITY + 1).next_power_of_two();
|
||||||
assert!(cap > capacity, "capacity overflow");
|
|
||||||
|
|
||||||
VecDeque { tail: 0, head: 0, buf: RawVec::with_capacity_in(cap, alloc) }
|
VecDeque { tail: 0, head: 0, buf: RawVec::with_capacity_in(cap, alloc) }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue