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

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

Performs the conversion.

Performs the conversion.

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Formats the value using the given formatter. Read more

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.