use super::reusable_objects::{ReusableObject, ReusableObjects};
pub use crate::containers::Vec;
pub use crate::containers::Vector;
pub type ReusableVecPool<T> = ReusableObjects<Vec<T>>;
use ::core::ops::{Index, IndexMut};
impl<T> IndexMut<usize> for ReusableObject<Vec<T>> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
unsafe { self.as_inner_mut().index_mut(index) }
}
}
impl<T> Index<usize> for ReusableObject<Vec<T>> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
self.as_ref().index(index)
}
}
impl<T> ReusableObject<Vec<T>> {
pub fn push(&mut self, value: T) -> bool {
unsafe { self.as_inner_mut().push(value).is_ok() }
}
pub fn pop(&mut self) -> Option<T> {
unsafe { self.as_inner_mut().pop() }
}
pub fn clear(&mut self) {
unsafe {
self.as_inner_mut().clear();
}
}
pub fn fill(&mut self, value: T)
where
T: Clone,
{
unsafe {
self.as_inner_mut().fill(value);
}
}
pub fn iter_mut(&mut self) -> ::core::slice::IterMut<'_, T> {
unsafe { self.as_inner_mut().iter_mut() }
}
}
#[cfg(test)]
#[cfg(not(loom))]
mod tests {
use crate::prelude::vector_extension::VectorExtension;
use super::*;
#[test]
fn test_push_and_index() {
let mut pool = ReusableVecPool::<i32>::new(2, |_| Vec::new_in_global(8));
let mut obj = pool.next_object().unwrap();
obj.push(10);
obj.push(20);
assert_eq!(obj[0], 10);
assert_eq!(obj[1], 20);
obj[1] = 99;
assert_eq!(obj[1], 99);
}
#[test]
fn test_pop_and_clear() {
let mut pool = ReusableVecPool::<i32>::new(1, |_| Vec::new_in_global(5));
let mut obj = pool.next_object().unwrap();
obj.push(1);
obj.push(2);
assert_eq!(obj.pop(), Some(2));
assert_eq!(obj.pop(), Some(1));
assert_eq!(obj.pop(), None);
obj.push(5);
obj.push(6);
obj.clear();
assert!(obj.pop().is_none());
}
#[test]
fn test_fill() {
let mut pool = ReusableVecPool::<i32>::new(1, |_| Vec::new_in_global(10));
let mut obj = pool.next_object().unwrap();
obj.fill(7);
for v in obj.iter() {
assert_eq!(*v, 7);
}
}
}