1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//! This crate provides RAII helpers for [`ash`]. In particular:
//!
//! * [`Guarded`]/[`GuardedResource`] is essentially a
//! [`ScopeGuard`](https://docs.rs/scopeguard/1.1.0/scopeguard/struct.ScopeGuard.html)
//! that selects an appropriate destructor automatically.
//! * [`DeviceExt`] (along with [`EntryExt`] and [`InstanceExt`]) provide convenience methods to
//! create resources and wrap them in [`GuardedResource`]s.
//! * [`Destroyable`] allows you to extend the behavior of [`GuardedResource`].
//!
//! # Introduction
//!
//! When working with Vulkan, you generally want to group multiple resources together into a few
//! large structs. Unfortunately, errors during initialization can leak resources:
//!
//! ```
//! # use ash::{prelude::VkResult, vk};
//! #
//! # struct Resources {
//! # render_pass: vk::RenderPass,
//! # pipeline_layout: vk::PipelineLayout,
//! # pipeline: vk::Pipeline,
//! # }
//! #
//! unsafe fn create_resources(device: &ash::Device) -> VkResult<Resources> {
//! let render_pass = create_render_pass(device)?;
//!
//! // BUG: Failure leaks render_pass
//! let pipeline_layout = create_pipeline_layout(device)?;
//!
//! // BUG: Failure leaks render_pass and pipeline_layout
//! let pipeline = create_pipeline(device, render_pass, pipeline_layout)?;
//!
//! Ok(Resources {
//! render_pass,
//! pipeline_layout,
//! pipeline,
//! })
//! }
//! # fn create_render_pass(_: &ash::Device) -> VkResult<vk::RenderPass> { unimplemented!() }
//! # fn create_pipeline_layout(_: &ash::Device) -> VkResult<vk::PipelineLayout> { unimplemented!() }
//! # fn create_pipeline(
//! # _: &ash::Device,
//! # _: vk::RenderPass,
//! # _: vk::PipelineLayout,
//! # ) -> VkResult<vk::Pipeline> {
//! # unimplemented!()
//! # }
//! ```
//!
//! It's straightforward to solve this with [`scopeguard`](https://docs.rs/scopeguard/), but it
//! tends to be a bit verbose and repetitive:
//!
//! ```
//! # use ash::{prelude::VkResult, vk};
//! use scopeguard::ScopeGuard;
//! # struct Resources {
//! # render_pass: vk::RenderPass,
//! # pipeline_layout: vk::PipelineLayout,
//! # pipeline: vk::Pipeline,
//! # }
//!
//! type Guarded<'a, T> = ScopeGuard<(T, &'a ash::Device), fn((T, &'a ash::Device))>;
//!
//! unsafe fn create_resources(device: &ash::Device) -> VkResult<Resources> {
//! let render_pass = create_render_pass(device)?;
//! let pipeline_layout = create_pipeline_layout(device)?;
//! let pipeline = create_pipeline(device, render_pass.0, pipeline_layout.0)?;
//! Ok(Resources {
//! render_pass: ScopeGuard::into_inner(render_pass).0,
//! pipeline_layout: ScopeGuard::into_inner(pipeline_layout).0,
//! pipeline: ScopeGuard::into_inner(pipeline).0,
//! })
//! }
//!
//! unsafe fn create_render_pass(device: &ash::Device) -> VkResult<Guarded<vk::RenderPass>> {
//! let create_info = unimplemented!();
//! let render_pass = device.create_render_pass(create_info, None)?;
//! Ok(scopeguard::guard(
//! (render_pass, device),
//! |(render_pass, device)| device.destroy_render_pass(render_pass, None),
//! ))
//! }
//!
//! // fn create_pipeline_layout(...) { ... }
//! // fn create_pipeline(...) { ... }
//!
//! # fn create_pipeline_layout(_: &ash::Device) -> VkResult<Guarded<vk::PipelineLayout>> {
//! # unimplemented!()
//! # }
//! # fn create_pipeline(
//! # _: &ash::Device,
//! # _: vk::RenderPass,
//! # _: vk::PipelineLayout,
//! # ) -> VkResult<Guarded<vk::Pipeline>> {
//! # unimplemented!()
//! # }
//! ```
//!
//! [`ashpan`](crate) reduces the friction of using [`scopeguard`](https://docs.rs/scopeguard/)
//! with [`ash`] by automatically selecting the destructor, passing the same
//! `allocation_callbacks` to the destructor that were used for resource creation and making
//! guarded resources convenient to extract:
//!
//! ```
//! # use ash::{prelude::VkResult, vk};
//! use ashpan::{DeviceExt, Guarded};
//! #
//! # struct Resources {
//! # render_pass: vk::RenderPass,
//! # pipeline_layout: vk::PipelineLayout,
//! # pipeline: vk::Pipeline,
//! # }
//!
//! unsafe fn create_resources(device: &ash::Device) -> VkResult<Resources> {
//! let render_pass = create_render_pass(device)?;
//! let pipeline_layout = create_pipeline_layout(device)?;
//! let pipeline = create_pipeline(device, *render_pass, *pipeline_layout)?;
//! Ok(Resources {
//! render_pass: render_pass.take(),
//! pipeline_layout: pipeline_layout.take(),
//! pipeline: pipeline.take(),
//! })
//! }
//!
//! unsafe fn create_render_pass(device: &ash::Device) -> VkResult<Guarded<vk::RenderPass>> {
//! let create_info = unimplemented!();
//! device.create_guarded_render_pass(create_info, None)
//! }
//!
//! // fn create_pipeline_layout(...) { ... }
//! // fn create_pipeline(...) { ... }
//!
//! # fn create_pipeline_layout(_: &ash::Device) -> VkResult<Guarded<vk::PipelineLayout>> {
//! # unimplemented!()
//! # }
//! # fn create_pipeline(
//! # _: &ash::Device,
//! # _: vk::RenderPass,
//! # _: vk::PipelineLayout,
//! # ) -> VkResult<Guarded<vk::Pipeline>> {
//! # unimplemented!()
//! # }
//! ```
//!
//! It's also possible to extend [`Guarded`] to handle application-specific types:
//!
//! ```
//! # use ash::{prelude::VkResult, vk};
//! use ashpan::{Destroyable, DeviceExt, Guarded};
//! #
//! # struct Resources {
//! # render_pass: vk::RenderPass,
//! # pipeline_layout: vk::PipelineLayout,
//! # pipeline: vk::Pipeline,
//! # }
//!
//! 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 create_resources(device: &ash::Device) -> VkResult<Guarded<Resources>> {
//! let resources = unimplemented!();
//! Ok(Guarded::new(resources, device, None))
//! }
//! ```
pub use Destroyable;
pub use DeviceExt;
pub use EntryExt;
pub use ;
pub use InstanceExt;