Struct LazyCollection

Source
pub struct LazyCollection<T: CheckEmpty> { /* private fields */ }
Available on crate feature 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>

Source

pub fn new(value: T) -> Self

Creates a new LazyCollection containing value.

§Examples
let c = LazyCollection::new(vec![42]);
Source

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));
Source

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);

Trait Implementations§

Source§

impl<T: Default + CheckEmpty> Default for LazyCollection<T>

Source§

fn default() -> LazyCollection<T>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for LazyCollection<T>

§

impl<T> !RefUnwindSafe for LazyCollection<T>

§

impl<T> Send for LazyCollection<T>
where T: Send,

§

impl<T> !Sync for LazyCollection<T>

§

impl<T> Unpin for LazyCollection<T>
where T: Unpin,

§

impl<T> UnwindSafe for LazyCollection<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Any for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

§

fn type_name(&self) -> &'static str

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.