Struct ashpan::GuardedResource [−][src]
pub struct GuardedResource<'a, Resource, Destroyer>(_)
where
Resource: Destroyable,
Destroyer: Deref<Target = <Resource as Destroyable>::Destroyer>;
Expand description
ScopeGuard tailored
for Vulkan
When the GuardedResource is dropped, the contained Resource is destroyed, generally by
calling an appropriate method on the Destroyer (usually an &ash::Device)
with allocation_callbacks. The contained resource can be accessed by dereferencing or
extracted with .take(). Application-specific types are supported if they
implement Destroyable. The Guarded alias is provided for the common use-case where
Destroyer is &ash::Device.
use ash::{prelude::VkResult, vk}; use ashpan::{DeviceExt, Guarded}; unsafe fn create_pipeline(device: &ash::Device) -> VkResult<Guarded<vk::Pipeline>> { let pipeline_cache = unimplemented!(); let create_info = unimplemented!(); // Because the returned pipelines are wrapped in a GuardedResource, // they don't leak when dropped by .map_err() let pipelines = device .create_guarded_graphics_pipelines(pipeline_cache, &[create_info], None) .map_err(|(_, err)| err)?; assert_eq!(pipelines.len(), 1); let pipeline = pipelines.take()[0]; // This would also work: // let pipeline = pipelines.pop().unwrap(); Ok(Guarded::new(pipeline, device, None)) }
Implementations
impl<'a, Resource, Destroyer> GuardedResource<'a, Resource, Destroyer> where
Resource: Destroyable,
Destroyer: Deref<Target = <Resource as Destroyable>::Destroyer>,
impl<'a, Resource, Destroyer> GuardedResource<'a, Resource, Destroyer> where
Resource: Destroyable,
Destroyer: Deref<Target = <Resource as Destroyable>::Destroyer>,
pub unsafe fn new(
resource: Resource,
destroyer: Destroyer,
allocation_callbacks: Option<&'a AllocationCallbacks>
) -> Self
pub unsafe fn new(
resource: Resource,
destroyer: Destroyer,
allocation_callbacks: Option<&'a AllocationCallbacks>
) -> Self
Creates a GuardedResource to hold the passed resource. destroyer and
allocation_callbacks are used during destruction.
Safety
You must ensure that it is safe to destroy resource when the GuardedResource is
dropped.
Extract the inner value without destroying it.
Note
Unlike
ScopeGuard::into_inner,
this is a method because it’s not intended to work with arbitrary types, so avoiding
shadowing .take() is less important than convenience.
Trait Implementations
impl<'a, Resource, Destroyer> AsMut<Resource> for GuardedResource<'a, Resource, Destroyer> where
Resource: Destroyable,
Destroyer: Deref<Target = <Resource as Destroyable>::Destroyer>,
impl<'a, Resource, Destroyer> AsMut<Resource> for GuardedResource<'a, Resource, Destroyer> where
Resource: Destroyable,
Destroyer: Deref<Target = <Resource as Destroyable>::Destroyer>,
impl<'a, Resource, Destroyer> AsRef<Resource> for GuardedResource<'a, Resource, Destroyer> where
Resource: Destroyable,
Destroyer: Deref<Target = <Resource as Destroyable>::Destroyer>,
impl<'a, Resource, Destroyer> AsRef<Resource> for GuardedResource<'a, Resource, Destroyer> where
Resource: Destroyable,
Destroyer: Deref<Target = <Resource as Destroyable>::Destroyer>,
impl<'a, Resource, Destroyer> Borrow<Resource> for GuardedResource<'a, Resource, Destroyer> where
Resource: Destroyable,
Destroyer: Deref<Target = <Resource as Destroyable>::Destroyer>,
impl<'a, Resource, Destroyer> Borrow<Resource> for GuardedResource<'a, Resource, Destroyer> where
Resource: Destroyable,
Destroyer: Deref<Target = <Resource as Destroyable>::Destroyer>,
impl<'a, Resource, Destroyer> BorrowMut<Resource> for GuardedResource<'a, Resource, Destroyer> where
Resource: Destroyable,
Destroyer: Deref<Target = <Resource as Destroyable>::Destroyer>,
impl<'a, Resource, Destroyer> BorrowMut<Resource> for GuardedResource<'a, Resource, Destroyer> where
Resource: Destroyable,
Destroyer: Deref<Target = <Resource as Destroyable>::Destroyer>,
Mutably borrows from an owned value. Read more