Trait ashpan::Destroyable[][src]

pub trait Destroyable {
    type Destroyer: ?Sized;
    unsafe fn destroy_with(
        &mut self,
        destroyer: &Self::Destroyer,
        allocation_callbacks: Option<&AllocationCallbacks>
    ); }
Expand description

Indicates that a type is destroyable

Vulkan resources generally need to be created and destroyed via an ash::Device or a Vulkan extension. The Destroyable trait provides a common interface, allowing compile-time selection of an appropriate destructor based on the resource type.

Implementing Destroyable for custom types makes them work with GuardedResource:

use ashpan::{Destroyable, DeviceExt, Guarded};

struct Resources {
    render_pass: vk::RenderPass,
    pipeline_layout: vk::PipelineLayout,
    pipeline: vk::Pipeline,
}

impl Resources {
    unsafe fn new(device: &ash::Device) -> VkResult<Guarded<Self>> {
        let resources = unimplemented!();
        Ok(Guarded::new(resources, device, None))
    }
}

impl Destroyable for Resources {
    type Destroyer = ash::Device;

    unsafe fn destroy_with(
        &mut self,
        device: &ash::Device,
        allocation_callbacks: Option<&vk::AllocationCallbacks>,
    ) {
        device.destroy_pipeline(self.pipeline, allocation_callbacks);
        device.destroy_pipeline_layout(self.pipeline_layout, allocation_callbacks);
        device.destroy_render_pass(self.render_pass, allocation_callbacks);
    }
}

// Elsewhere...
unsafe fn build_and_frob_resources(device: &ash::Device) -> VkResult<()> {
    let resources = Resources::new(device)?;
    frob_resources(&*resources)?;
    Ok(())
}

fn frob_resources(resources: &Resources) -> VkResult<()> {
    unimplemented!()
}

Associated Types

The type that performs the destruction of the Destroyable

Required methods

Destroys self via destroyer with allocation_callbacks.

Safety

Depends on the resource type; see Vulkan spec for details.

Implementations on Foreign Types

Implementors