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 @@
#[macro_export] pub struct Cleanup<T: Fn()> {
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, func: T,
} }
impl<T> Drop for $name <T> where T: Fn() {
impl<T> Drop for Cleanup<T>
where
T: Fn(),
{
fn drop(&mut self) { fn drop(&mut self) {
(self.func)(); (self.func)();
} }
} }
impl<T> $name <T> where T: Fn() {
fn new(func: T) -> Self { impl<T> Cleanup<T>
Self { where
func, T: Fn(),
} {
pub fn new(func: T) -> Self {
Self { func }
} }
} }
$hold_name = $name::new($cleanup_fn); #[macro_export]
macro_rules! cleanup_func {
(func: $cleanup_fn:expr,
hold_name: $hold_name:ident,
$(var_pair: $orig_var:expr, $new_var:ident),*) => {
$(let $new_var = $orig_var;)*
$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::vkDestroyBuffer(device_copy, staging_buf_copy, std::ptr::null());
ffi::vkFreeMemory(device_copy, staging_buf_mem_copy, std::ptr::null()); ffi::vkFreeMemory(device_copy, staging_buf_mem_copy, std::ptr::null());
}, },
name: CleanupStaging,
hold_name: _inst, hold_name: _inst,
var_pair: self.device, device_copy, var_pair: self.device, device_copy,
var_pair: staging_buffer, staging_buf_copy, var_pair: staging_buffer, staging_buf_copy,
@ -1883,7 +1882,6 @@ impl VulkanApp {
ffi::vkDestroyBuffer(device_copy, buf_copy, std::ptr::null()); ffi::vkDestroyBuffer(device_copy, buf_copy, std::ptr::null());
ffi::vkFreeMemory(device_copy, buf_mem_copy, std::ptr::null()); ffi::vkFreeMemory(device_copy, buf_mem_copy, std::ptr::null());
}, },
name: CleanupIdxStaging,
hold_name: _staging_inst, hold_name: _staging_inst,
var_pair: self.device, device_copy, var_pair: self.device, device_copy,
var_pair: buf, buf_copy, var_pair: buf, buf_copy,