pub struct LazyCollection<T: CheckEmpty> { /* private fields */ }unstable only.Expand description
A collection wrapper, that guarantees that an empty collection can be trivially destructed.
This makes it easy to use collections in a global object that requires trivially destructible. When using global objects in a dynamic link library that allow a process to load and unload the dynamic link library multiple times, no OS provides reliable way to teardown the global resources allocated, so all global resources used should be trivially destructible.
This is especially true for an Android Vulkan layer. When querying the capabilities of Vulkan layers, the Android Vulkan loader will load the shared object and call into the exposed introspection queries, and unload the shared object once the task is done. The Android Vulkan loader may load the shared object later again if the layer is activated. However, on Android there is no reliable way to register a callback when the shared object is actually unloaded.
Similar to RefCell, even if T implements Sync, LazyCollection<T> is not Sync,
because a single-threaded way is used to test if the underlying collection is empty and destroy
the allocation
Implementations§
Source§impl<T: CheckEmpty> LazyCollection<T>
impl<T: CheckEmpty> LazyCollection<T>
Sourcepub fn get(&self) -> Cow<'_, T>
pub fn get(&self) -> Cow<'_, T>
Gets the reference to the underlying collection. Returns an owned empty T if the underlying collection is empty.
§Examples
let vec = LazyCollection::new(vec![42]);
let vec1 = vec.get();
assert_eq!(*vec1, vec![42]);
let vec2 = vec.get();
// vec1 and vec2 point to the same location.
assert!(std::ptr::eq(&*vec1, &*vec2));Sourcepub fn get_mut_or_default(&mut self) -> CollectionRefMut<'_, T>
pub fn get_mut_or_default(&mut self) -> CollectionRefMut<'_, T>
Gets a mutable reference to the underlying collection, create an empty collection if the underlying collection was empty.
§Examples
let mut vec = LazyCollection::<Vec<u32>>::default();
let mut mut_vec = vec.get_mut_or_default();
mut_vec.push(42);
assert_eq!(*mut_vec, vec![42]);
drop(mut_vec);
let mut mut_vec = vec.get_mut_or_default();
mut_vec.remove(0);
assert_eq!(*mut_vec, vec![]);
drop(mut_vec);
// This won't cause a memory leak.
std::mem::forget(vec);