1use core::cell::UnsafeCell;
2
3pub(crate) type LazyResult<T> = LazyCell<Result<T, crate::Error>>;
4
5pub(crate) struct LazyCell<T> {
6 contents: UnsafeCell<Option<T>>,
7}
8
9impl<T> LazyCell<T> {
10 pub(crate) fn new() -> LazyCell<T> {
11 LazyCell {
12 contents: UnsafeCell::new(None),
13 }
14 }
15
16 pub(crate) fn borrow(&self) -> Option<&T> {
17 unsafe { &*self.contents.get() }.as_ref()
18 }
19
20 pub(crate) fn borrow_with(&self, closure: impl FnOnce() -> T) -> &T {
21 let ptr = self.contents.get();
23 if let Some(val) = unsafe { &*ptr } {
24 return val;
25 }
26 let val = closure();
32 unsafe { (*ptr).get_or_insert(val) }
33 }
34}