diff --git a/nixide/src/expr/realised_string.rs b/nixide/src/expr/realised_string.rs index 5be5f8b..a3dbc7c 100644 --- a/nixide/src/expr/realised_string.rs +++ b/nixide/src/expr/realised_string.rs @@ -13,9 +13,9 @@ use crate::{EvalState, NixideResult, StorePath}; pub struct RealisedString { inner: NonNull, - // pub path: LazyCell StorePath>>, pub path: StorePath, - pub children: LazyArray StorePath>, usize) -> StorePath>>, + pub children: + LazyArray StorePath>, usize) -> StorePath>, } impl AsInnerPtr for RealisedString { @@ -88,7 +88,6 @@ impl RealisedString { Ok(Self { inner, path: Self::parse_path(inner.as_ptr(), state), - // children: LazyArray::new(size, delegate as fn(usize) -> StorePath), children: LazyArray:: StorePath>>::new( size, Box::new(delegate), diff --git a/nixide/src/stdext/mod.rs b/nixide/src/stdext/mod.rs index fe17913..d30c295 100644 --- a/nixide/src/stdext/mod.rs +++ b/nixide/src/stdext/mod.rs @@ -1,4 +1,4 @@ mod cchar_ptr_ext; -pub(crate) use cchar_ptr_ext::CCharPtrExt; +pub(crate) use cchar_ptr_ext::{AsCPtr, CCharPtrExt}; pub(crate) use stdext::*; diff --git a/nixide/src/util/lazy_array.rs b/nixide/src/util/lazy_array.rs index 2808314..6b4b1a5 100644 --- a/nixide/src/util/lazy_array.rs +++ b/nixide/src/util/lazy_array.rs @@ -1,12 +1,11 @@ -use std::cell::RefCell; -use std::rc::Rc; +use std::cell::{Ref, RefCell}; #[derive(Debug)] pub struct LazyArray where F: Fn(usize) -> T, { - inner: Rc>>>, + inner: RefCell>>, size: usize, delegate: F, } @@ -22,58 +21,25 @@ where } LazyArray { - inner: Rc::new(RefCell::new(vec)), + inner: RefCell::new(vec), size, delegate, } } - /// Returns `None` if `index < self.size` otherwise always succeeds + /// Returns `None` if `index >= self.size` otherwise always succeeds /// (unless of course the callback you supply panics). /// - // pub fn get(&mut self, index: usize) -> Option<&T> { - // // let x = self.inner.get(index).copied().and_then(|value| match value { - // // Some(value) => Some(value), - // // None => { - // // // store the value first - // // let value = (self.delegate)(index); - // // self.inner[index] = Some(value); + pub fn get<'a>(&'a mut self, index: usize) -> Option>> { + let borrowed = self.inner.borrow(); - // // // now get a reference to it - // // if let Some(v) = &self.inner[index] { - // // return Some(v); - // // } - // // None - // // } - // // }) - // match self.inner.clone().borrow().get(index) { - // Some(Some(value)) => Some(value), - // Some(None) => { - // let mut inner = self.inner.clone().borrow_mut(); - // // store the value first - // inner[index] = Some((self.delegate)(index)); - - // // now get a reference to it - // inner[index].as_ref() - // } - // None => None, - // } - // } - pub fn get(&mut self, index: usize) -> Option> { if index >= self.size { return None; + } else if borrowed[index].is_none() { + let value = (self.delegate)(index); + self.inner.borrow_mut()[index] = Some(value); } - // let inner = self.inner.borrow(); - if let Some(value) = self.inner.borrow()[index].as_ref() { - return Some(Rc::new(value)); - } - - // drop(inner); // explicitly drop the borrow - - let value = (self.delegate)(index); - self.inner.borrow_mut()[index] = Some(value); - - Some(Rc::new(self.inner.borrow()[index].unwrap())) + Some(Ref::map(borrowed, |v| &v[index])) } }