From 707ce86b3f9b5c160644bc1da15aa2c485621044 Mon Sep 17 00:00:00 2001 From: Adam Cimarosti Date: Mon, 30 Sep 2024 10:49:11 +0100 Subject: [PATCH] try_with_capacity_in --- src/vec.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/vec.rs b/src/vec.rs index e3f15e9..c82babb 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -21,6 +21,11 @@ impl Vec { self.inner.allocator() } + #[inline] + pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result { + Ok(Self { inner: InnerVec::try_with_capacity_in(capacity, alloc)? }) + } + #[inline] pub fn capacity(&self) -> usize { self.inner.capacity() @@ -545,6 +550,16 @@ mod tests { assert_eq!(wma.in_use(), 0); } + #[test] + fn try_with_capacity_in() { + let wma = WatermarkAllocator::new(64); + let vec: Vec = 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);