/rust/registry/src/github.com-1ecc6299db9ec823/addr2line-0.14.1/src/lazy.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use core::cell::UnsafeCell; |
2 | | |
3 | | pub struct LazyCell<T> { |
4 | | contents: UnsafeCell<Option<T>>, |
5 | | } |
6 | | impl<T> LazyCell<T> { |
7 | | pub fn new() -> LazyCell<T> { |
8 | | LazyCell { |
9 | | contents: UnsafeCell::new(None), |
10 | | } |
11 | | } |
12 | | |
13 | 0 | pub fn borrow_with(&self, closure: impl FnOnce() -> T) -> &T { |
14 | 0 | unsafe { |
15 | 0 | // First check if we're already initialized... |
16 | 0 | let ptr = self.contents.get(); |
17 | 0 | if let Some(val) = &*ptr { |
18 | 0 | return val; |
19 | 0 | } |
20 | 0 | // Note that while we're executing `closure` our `borrow_with` may |
21 | 0 | // be called recursively. This means we need to check again after |
22 | 0 | // the closure has executed. For that we use the `get_or_insert` |
23 | 0 | // method which will only perform mutation if we aren't already |
24 | 0 | // `Some`. |
25 | 0 | let val = closure(); |
26 | 0 | (*ptr).get_or_insert(val) |
27 | | } |
28 | 0 | } Unexecuted instantiation: <addr2line::lazy::LazyCell<core::result::Result<addr2line::Function<gimli::read::endian_slice::EndianSlice<gimli::endianity::LittleEndian>>, gimli::read::Error>>>::borrow_with::<<addr2line::ResUnit<gimli::read::endian_slice::EndianSlice<gimli::endianity::LittleEndian>>>::find_function_or_location::{closure#0}>Unexecuted instantiation: <addr2line::lazy::LazyCell<core::result::Result<addr2line::Lines, gimli::read::Error>>>::borrow_with::<<addr2line::ResUnit<gimli::read::endian_slice::EndianSlice<gimli::endianity::LittleEndian>>>::parse_lines::{closure#0}>Unexecuted instantiation: <addr2line::lazy::LazyCell<core::result::Result<addr2line::Functions<gimli::read::endian_slice::EndianSlice<gimli::endianity::LittleEndian>>, gimli::read::Error>>>::borrow_with::<<addr2line::ResUnit<gimli::read::endian_slice::EndianSlice<gimli::endianity::LittleEndian>>>::parse_functions::{closure#0}> |
29 | | } |