diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:41 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:41 +0000 |
commit | 4f9fe856a25ab29345b90e7725509e9ee38a37be (patch) | |
tree | e4ffd8a9374cae7b21f7cbfb352927e0e074aff6 /vendor/addr2line-0.17.0/src/lazy.rs | |
parent | Adding upstream version 1.68.2+dfsg1. (diff) | |
download | rustc-5cd5bd4daf55da04d2c8e7c06c3067a027cfbfc2.tar.xz rustc-5cd5bd4daf55da04d2c8e7c06c3067a027cfbfc2.zip |
Adding upstream version 1.69.0+dfsg1.upstream/1.69.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | vendor/addr2line-0.17.0/src/lazy.rs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/vendor/addr2line-0.17.0/src/lazy.rs b/vendor/addr2line-0.17.0/src/lazy.rs new file mode 100644 index 000000000..280c76b46 --- /dev/null +++ b/vendor/addr2line-0.17.0/src/lazy.rs @@ -0,0 +1,29 @@ +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_with(&self, closure: impl FnOnce() -> T) -> &T { + unsafe { + // First check if we're already initialized... + let ptr = self.contents.get(); + if let Some(val) = &*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(); + (*ptr).get_or_insert(val) + } + } +} |