From d710719cae8d79ffcca8a6467f9fac678636d155 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Tue, 18 Apr 2023 18:11:04 -0700 Subject: [PATCH] Add some convenience functions to list pool impl. --- src/pool.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/pool.rs b/src/pool.rs index 58dcd2a..1a17ab8 100644 --- a/src/pool.rs +++ b/src/pool.rs @@ -36,9 +36,28 @@ impl ListPool { pub fn single(&mut self, value: T) -> ListRef { self.from_iter(std::iter::once(value)) } + pub fn double(&mut self, a: T, b: T) -> ListRef { + self.from_iter(std::iter::once(a).chain(std::iter::once(b))) + } + pub fn triple(&mut self, a: T, b: T, c: T) -> ListRef { + 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 { self.from_iter(std::iter::repeat(initial).take(size)) } + pub fn deep_clone(&mut self, list: ListRef) -> ListRef { + 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 Index> for ListPool {