demo_vk/graphics/vulkan/raii/
instance.rs

1use {crate::unwrap_here, anyhow::Result, ash::vk, std::sync::Arc};
2
3/// A RAII wrapper for the ash library entry and instance that destroys itself
4/// when dropped.
5pub struct Instance {
6    pub entry: ash::Entry,
7    pub raw: ash::Instance,
8}
9
10impl Instance {
11    pub fn new(create_info: &vk::InstanceCreateInfo) -> Result<Arc<Self>> {
12        let entry = unwrap_here!("Create the Vulkan loader", unsafe {
13            ash::Entry::load()
14        });
15        let raw = unwrap_here!("Create the Vulkan library instance", unsafe {
16            entry.create_instance(create_info, None)
17        });
18        Ok(Arc::new(Self { entry, raw }))
19    }
20}
21
22impl std::ops::Deref for Instance {
23    type Target = ash::Instance;
24
25    fn deref(&self) -> &Self::Target {
26        &self.raw
27    }
28}
29
30impl Drop for Instance {
31    fn drop(&mut self) {
32        unsafe {
33            self.raw.destroy_instance(None);
34        }
35    }
36}
37
38impl std::fmt::Debug for Instance {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        f.debug_struct("<Ash Library Instance>").finish()
41    }
42}