From 5898df63eb20212e3d90306e579f6634724e0753 Mon Sep 17 00:00:00 2001 From: Adam Cimarosti Date: Wed, 25 Sep 2024 11:42:36 +0100 Subject: [PATCH] clear, capacity and as_slice --- .gitignore | 1 + src/vec.rs | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 2b228c0..aa25a23 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ target/ Cargo.lock **/*.rs.bk *.pdb +tarpaulin-report.html \ No newline at end of file diff --git a/src/vec.rs b/src/vec.rs index d97c2ed..baf25dc 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -17,6 +17,11 @@ impl Vec { } } + #[inline] + pub fn capacity(&self) -> usize { + self.inner.capacity() + } + #[inline] pub fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { self.inner.try_reserve(additional) @@ -62,6 +67,11 @@ impl Vec { self.inner.is_empty() } + #[inline] + pub fn as_slice(&self) -> &[T] { + self + } + #[inline] pub fn as_ptr(&self) -> *const T { self.inner.as_ptr() @@ -71,6 +81,11 @@ impl Vec { pub fn as_mut_ptr(&mut self) -> *mut T { self.inner.as_mut_ptr() } + + #[inline] + pub fn clear(&mut self) { + self.inner.clear(); + } } impl Vec { @@ -186,10 +201,11 @@ mod tests { } #[test] - fn test_push() { + fn test_basics() { let wma = WatermarkAllocator::new(32); let mut vec = Vec::new_in(wma); assert_eq!(vec.len(), 0); + assert_eq!(vec.capacity(), 0); assert!(vec.is_empty()); vec.push(1).unwrap(); assert_eq!(vec.len(), 1); @@ -198,9 +214,14 @@ mod tests { vec.push(3).unwrap(); vec.push(4).unwrap(); assert_eq!(vec.len(), 4); + assert_eq!(vec.capacity(), 4); let _err: TryReserveError = vec.push(5).unwrap_err(); - assert_eq!(vec.inner.as_slice(), &[1, 2, 3, 4]); + assert_eq!(vec.as_slice(), &[1, 2, 3, 4]); assert_eq!(vec.len(), 4); + vec.clear(); + assert_eq!(vec.len(), 0); + assert!(vec.is_empty()); + assert_eq!(vec.capacity(), 4); } #[test]