From 88a4d3df13f543a17fffbd5eb00ce49cb9ad2720 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 20 Mar 2024 19:13:16 +0900 Subject: [PATCH] Refactor helper::cleanup_func! macro --- src/helper.rs | 41 +++++++++++++++++++++++------------------ src/main.rs | 2 -- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/helper.rs b/src/helper.rs index 32e3205..949e7ef 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -1,27 +1,32 @@ +pub struct Cleanup { + func: T, +} + +impl Drop for Cleanup +where + T: Fn(), +{ + fn drop(&mut self) { + (self.func)(); + } +} + +impl Cleanup +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 { - func: T, - } - impl Drop for $name where T: Fn() { - fn drop(&mut self) { - (self.func)(); - } - } - impl $name where T: Fn() { - fn new(func: T) -> Self { - Self { - func, - } - } - } - - $hold_name = $name::new($cleanup_fn); + $hold_name = crate::helper::Cleanup::new($cleanup_fn); } } diff --git a/src/main.rs b/src/main.rs index be9e690..124fc99 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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,