use std::collections::TryReserveError;
pub trait TrySetCapacity {
fn try_set_capacity(&mut self, capacity: usize) -> Result<(), TryReserveError>;
}
pub trait TrySetCapacityExact {
fn try_set_capacity_exact(&mut self, capacity: usize) -> Result<(), TryReserveError>;
}
impl<T> TrySetCapacity for Vec<T> {
fn try_set_capacity(&mut self, capacity: usize) -> Result<(), TryReserveError> {
let additional = capacity.saturating_sub(self.len());
self.try_reserve(additional)
}
}
impl<T> TrySetCapacityExact for Vec<T> {
fn try_set_capacity_exact(&mut self, capacity: usize) -> Result<(), TryReserveError> {
let additional = capacity.saturating_sub(self.len());
self.try_reserve_exact(additional)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_try_set_capacity() {
let mut v = vec![1, 2, 3];
assert_eq!(v.capacity(), 3);
v.try_set_capacity(5).unwrap();
assert_eq!(v.capacity(), 6);
v.try_set_capacity(5).unwrap();
assert_eq!(v.capacity(), 6);
v.extend([4, 5, 6]);
assert_eq!(v.capacity(), 6);
v.push(7);
assert_eq!(v.capacity(), 12);
v.try_set_capacity(100).unwrap();
assert_eq!(v.capacity(), 100);
v.try_set_capacity(5).unwrap();
assert_eq!(v.capacity(), 100);
}
#[test]
fn test_try_set_capacity_exact() {
let mut v = vec![1, 2, 3];
assert_eq!(v.capacity(), 3);
v.try_set_capacity_exact(5).unwrap();
assert_eq!(v.capacity(), 5);
v.try_set_capacity_exact(5).unwrap();
assert_eq!(v.capacity(), 5);
v.extend([4, 5]);
assert_eq!(v.capacity(), 5);
v.push(6);
assert_eq!(v.capacity(), 10);
v.try_set_capacity_exact(100).unwrap();
assert_eq!(v.capacity(), 100);
v.try_set_capacity_exact(5).unwrap();
assert_eq!(v.capacity(), 100);
}
}