diff options
author | Valentin Popov <valentin@popov.link> | 2024-01-08 00:21:28 +0300 |
---|---|---|
committer | Valentin Popov <valentin@popov.link> | 2024-01-08 00:21:28 +0300 |
commit | 1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch) | |
tree | 7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/addr2line/src/lazy.rs | |
parent | 5ecd8cf2cba827454317368b68571df0d13d7842 (diff) | |
download | fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip |
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/addr2line/src/lazy.rs')
-rw-r--r-- | vendor/addr2line/src/lazy.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/vendor/addr2line/src/lazy.rs b/vendor/addr2line/src/lazy.rs new file mode 100644 index 0000000..2df2ed6 --- /dev/null +++ b/vendor/addr2line/src/lazy.rs @@ -0,0 +1,31 @@ +use core::cell::UnsafeCell; + +pub struct LazyCell<T> { + contents: UnsafeCell<Option<T>>, +} +impl<T> LazyCell<T> { + pub fn new() -> LazyCell<T> { + LazyCell { + contents: UnsafeCell::new(None), + } + } + + pub fn borrow(&self) -> Option<&T> { + unsafe { &*self.contents.get() }.as_ref() + } + + pub fn borrow_with(&self, closure: impl FnOnce() -> T) -> &T { + // First check if we're already initialized... + let ptr = self.contents.get(); + if let Some(val) = unsafe { &*ptr } { + return val; + } + // Note that while we're executing `closure` our `borrow_with` may + // be called recursively. This means we need to check again after + // the closure has executed. For that we use the `get_or_insert` + // method which will only perform mutation if we aren't already + // `Some`. + let val = closure(); + unsafe { (*ptr).get_or_insert(val) } + } +} |