Refactor helper::cleanup_func! macro

This commit is contained in:
Stephen Seo 2024-03-20 19:13:16 +09:00
parent 77f4a980b9
commit 88a4d3df13
2 changed files with 23 additions and 20 deletions

View file

@ -1,27 +1,32 @@
pub struct Cleanup<T: Fn()> {
func: T,
}
impl<T> Drop for Cleanup<T>
where
T: Fn(),
{
fn drop(&mut self) {
(self.func)();
}
}
impl<T> Cleanup<T>
where
T: Fn(),
{
pub fn new(func: T) -> Self {
Self { func }
}
}
#[macro_export]
macro_rules! cleanup_func {
(func: $cleanup_fn:expr,
name: $name:ident,
hold_name: $hold_name:ident,
$(var_pair: $orig_var:expr, $new_var:ident),*) => {
$(let $new_var = $orig_var;)*
struct $name <T: Fn()> {
func: T,
}
impl<T> Drop for $name <T> where T: Fn() {
fn drop(&mut self) {
(self.func)();
}
}
impl<T> $name <T> where T: Fn() {
fn new(func: T) -> Self {
Self {
func,
}
}
}
$hold_name = $name::new($cleanup_fn);
$hold_name = crate::helper::Cleanup::new($cleanup_fn);
}
}

View file

@ -1643,7 +1643,6 @@ impl VulkanApp {
ffi::vkDestroyBuffer(device_copy, staging_buf_copy, std::ptr::null());
ffi::vkFreeMemory(device_copy, staging_buf_mem_copy, std::ptr::null());
},
name: CleanupStaging,
hold_name: _inst,
var_pair: self.device, device_copy,
var_pair: staging_buffer, staging_buf_copy,
@ -1883,7 +1882,6 @@ impl VulkanApp {
ffi::vkDestroyBuffer(device_copy, buf_copy, std::ptr::null());
ffi::vkFreeMemory(device_copy, buf_mem_copy, std::ptr::null());
},
name: CleanupIdxStaging,
hold_name: _staging_inst,
var_pair: self.device, device_copy,
var_pair: buf, buf_copy,