Add some convenience functions to list pool impl.
This commit is contained in:
parent
dab18103ce
commit
d710719cae
19
src/pool.rs
19
src/pool.rs
|
@ -36,9 +36,28 @@ impl<T: Clone + Debug> ListPool<T> {
|
|||
pub fn single(&mut self, value: T) -> ListRef<T> {
|
||||
self.from_iter(std::iter::once(value))
|
||||
}
|
||||
pub fn double(&mut self, a: T, b: T) -> ListRef<T> {
|
||||
self.from_iter(std::iter::once(a).chain(std::iter::once(b)))
|
||||
}
|
||||
pub fn triple(&mut self, a: T, b: T, c: T) -> ListRef<T> {
|
||||
self.from_iter(
|
||||
std::iter::once(a)
|
||||
.chain(std::iter::once(b))
|
||||
.chain(std::iter::once(c)),
|
||||
)
|
||||
}
|
||||
pub fn allocate(&mut self, size: usize, initial: T) -> ListRef<T> {
|
||||
self.from_iter(std::iter::repeat(initial).take(size))
|
||||
}
|
||||
pub fn deep_clone(&mut self, list: ListRef<T>) -> ListRef<T> {
|
||||
self.storage.reserve(list.len());
|
||||
let start = u32::try_from(self.storage.len()).unwrap();
|
||||
for i in list.0..list.1 {
|
||||
self.storage.push(self.storage[i as usize].clone());
|
||||
}
|
||||
let end = u32::try_from(self.storage.len()).unwrap();
|
||||
ListRef(start, end, PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone + Debug> Index<ListRef<T>> for ListPool<T> {
|
||||
|
|
Loading…
Reference in a new issue