Skip to content

Commit

Permalink
clear, capacity and as_slice
Browse files Browse the repository at this point in the history
  • Loading branch information
amunra committed Sep 25, 2024
1 parent ec76293 commit 5898df6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ target/
Cargo.lock
**/*.rs.bk
*.pdb
tarpaulin-report.html
25 changes: 23 additions & 2 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ impl<T, A: Allocator> Vec<T, A> {
}
}

#[inline]
pub fn capacity(&self) -> usize {
self.inner.capacity()
}

#[inline]
pub fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
self.inner.try_reserve(additional)
Expand Down Expand Up @@ -62,6 +67,11 @@ impl<T, A: Allocator> Vec<T, A> {
self.inner.is_empty()
}

#[inline]
pub fn as_slice(&self) -> &[T] {
self
}

#[inline]
pub fn as_ptr(&self) -> *const T {
self.inner.as_ptr()
Expand All @@ -71,6 +81,11 @@ impl<T, A: Allocator> Vec<T, A> {
pub fn as_mut_ptr(&mut self) -> *mut T {
self.inner.as_mut_ptr()
}

#[inline]
pub fn clear(&mut self) {
self.inner.clear();
}
}

impl<T: Clone, A: Allocator> Vec<T, A> {
Expand Down Expand Up @@ -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);
Expand All @@ -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]
Expand Down

0 comments on commit 5898df6

Please sign in to comment.