Skip to content

Commit

Permalink
try_with_capacity_in
Browse files Browse the repository at this point in the history
  • Loading branch information
amunra committed Sep 30, 2024
1 parent 0f04f08 commit b3e307a
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ impl<T, A: Allocator> Vec<T, A> {
self.inner.allocator()
}

#[inline]
pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> {
Ok(Self {
inner: InnerVec::try_with_capacity_in(capacity, alloc)?,
})
}

#[inline]
pub fn capacity(&self) -> usize {
self.inner.capacity()
Expand Down Expand Up @@ -545,6 +552,16 @@ mod tests {
assert_eq!(wma.in_use(), 0);
}

#[test]
fn try_with_capacity_in() {
let wma = WatermarkAllocator::new(64);
let vec: Vec<u8, _> = Vec::try_with_capacity_in(64, wma.clone()).unwrap();
assert_eq!(vec.len(), 0);
assert_eq!(vec.as_slice(), &[]);
assert_eq!(vec.capacity(), 64);
assert_eq!(wma.in_use(), 64);
}

#[test]
fn test_truncate() {
let wma = WatermarkAllocator::new(32);
Expand Down

0 comments on commit b3e307a

Please sign in to comment.