Skip to content

Commit 9bb2747

Browse files
dvadymcopybara-github
authored andcommitted
Implement methods in Vector for changing capacity (reserve and shrink variations)
PiperOrigin-RevId: 696960440 Change-Id: Icc1231cd2353419dacd4b62192163527a90afe43
1 parent f0f4266 commit 9bb2747

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

support/cc_std/test/vector/test.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,67 @@ fn test_vector_reserve() {
285285
let mut v = vector::Vector::<i32>::new();
286286
v.reserve(10);
287287
expect_that!(v.capacity(), ge(10));
288+
let current_capacity = v.capacity();
289+
v.reserve(5); // 5 < 10, so it should do nothing.
290+
expect_eq!(v.capacity(), current_capacity);
291+
}
292+
293+
#[gtest]
294+
fn test_vector_reserve_exact() {
295+
let mut v = vector::Vector::<usize>::new();
296+
v.reserve_exact(10);
297+
expect_that!(v.capacity(), ge(10));
298+
let current_capacity = v.capacity();
299+
// Add elements to the current capacity.
300+
for i in v.len()..current_capacity {
301+
v.push(i);
302+
}
303+
v.reserve_exact(5); // it ensures that the capacity for additional 5 elements.
304+
expect_that!(v.capacity(), ge(current_capacity + 5));
305+
}
306+
307+
#[gtest]
308+
fn test_vector_try_reserve() {
309+
let mut v = vector::Vector::<i32>::new();
310+
v.try_reserve(100).unwrap();
311+
expect_that!(v.capacity(), ge(100));
312+
}
313+
314+
#[gtest]
315+
fn test_vector_try_reserve_exact() {
316+
let mut v = vector::Vector::<usize>::new();
317+
v.try_reserve_exact(10).unwrap();
318+
expect_that!(v.capacity(), ge(10));
319+
let current_capacity = v.capacity();
320+
// Add elements to the current capacity.
321+
for i in v.len()..current_capacity {
322+
v.push(i);
323+
}
324+
v.try_reserve_exact(5).unwrap(); // it ensures that the capacity for additional 5 elements.
325+
expect_that!(v.capacity(), ge(current_capacity + 5));
326+
}
327+
328+
#[gtest]
329+
fn test_shrink_to_fit() {
330+
let mut v = vector::Vector::<i32>::new();
331+
for i in 0..100 {
332+
v.push(i);
333+
}
334+
v.clear(); // doesn't shrink the capacity.
335+
v.shrink_to_fit();
336+
expect_eq!(v.capacity(), 0);
337+
}
338+
339+
#[gtest]
340+
fn test_shrink_to() {
341+
let mut v = vector::Vector::<i32>::new();
342+
for i in 0..100 {
343+
v.push(i);
344+
}
345+
v.clear(); // doesn't shrink the capacity.
346+
v.shrink_to(10);
347+
expect_that!(v.capacity(), le(99));
348+
expect_that!(v.capacity(), ge(10));
288349
}
289350

290351
#[gtest]

support/cc_std/vector.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44
#![feature(allocator_api)]
55
#![feature(cfg_sanitize)]
6+
use std::collections::TryReserveError;
67
#[cfg(sanitize = "address")]
78
use std::ffi::c_void;
89
use std::mem::ManuallyDrop;
@@ -161,10 +162,31 @@ impl<T: Unpin> Vector<T> {
161162
}
162163
}
163164

165+
// Methods for changing the capacity of the vector.
164166
pub fn reserve(&mut self, capacity: usize) {
165167
self.mutate_self_as_vec(|v| v.reserve(capacity));
166168
}
167169

170+
pub fn reserve_exact(&mut self, additional: usize) {
171+
self.mutate_self_as_vec(|v| v.reserve_exact(additional));
172+
}
173+
174+
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
175+
self.mutate_self_as_vec(|v| v.try_reserve(additional))
176+
}
177+
178+
pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
179+
self.mutate_self_as_vec(|v| v.try_reserve_exact(additional))
180+
}
181+
182+
pub fn shrink_to_fit(&mut self) {
183+
self.mutate_self_as_vec(|v| v.shrink_to_fit());
184+
}
185+
186+
pub fn shrink_to(&mut self, min_capacity: usize) {
187+
self.mutate_self_as_vec(|v| v.shrink_to(min_capacity));
188+
}
189+
168190
pub fn with_capacity(capacity: usize) -> Vector<T> {
169191
let mut result = Vector::new();
170192
result.reserve(capacity);

0 commit comments

Comments
 (0)