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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
use std::ops::Deref;

/// Holds a resource and a free-closure that is called when the guard is dropped.
///
/// Allows to couple resource acquisition and freeing, while treating the guard as the contained resource and ensuring freeing will happen. When writing the code, it's also nice to transfer the documentation into everything that has to happen in one go without having to split it into upper and lower or here- and there-code. In a function, Rust's drop order should ensure that later aquired resources are freed first.
pub struct ResGuard<R, F>
where
    F: FnOnce(R),
{
    resource: Option<R>,
    free: Option<F>,
}

impl<R, F> ResGuard<R, F>
where
    F: FnOnce(R),
{
    pub fn new(resource: R, free: F) -> Self {
        //! Should normally not be needed.

        Self {
            resource: Some(resource),
            free: Some(free),
        }
    }

    pub fn with_acquisition<A, E>(acquire: A, free: F) -> Result<Self, E>
    where
        A: FnOnce() -> Result<R, E>,
    {
        //! For functions that return the resource.

        Ok(Self {
            resource: Some(acquire()?),
            free: Some(free),
        })
    }

    pub fn with_mut_acquisition<A, T, E>(acquire: A, free: F) -> Result<Self, E>
    where
        A: FnOnce(&mut R) -> Result<T, E>,
        R: Default,
    {
        //! For functions that provide the resource by means of an out-parameter.

        let mut resource = R::default();
        acquire(&mut resource)?;

        Ok(Self {
            resource: Some(resource),
            free: Some(free),
        })
    }
}

macro_rules! impl_with_acq_and_star {
    ($feature:expr, $type:ty, $acq:ident, $acq_mut:ident, $free_fn:expr) => {
        #[cfg(feature = $feature)]
        impl ResGuard<$type, fn($type)> {
            #[doc = concat!("Activate feature `", $feature, "`.")]
            pub fn $acq<A, E>(acquire: A) -> Result<ResGuard<$type, fn($type)>, E>
            where
                A: FnOnce() -> Result<$type, E>,
            {
                Self::with_acquisition(acquire, $free_fn)
            }

            /// Activate same feature.
            pub fn $acq_mut<A, T, E>(acquire: A) -> Result<ResGuard<$type, fn($type)>, E>
            where
                A: FnOnce(&mut $type) -> Result<T, E>,
            {
                Self::with_mut_acquisition(acquire, $free_fn)
            }
        }
    };
}

// Note: The impls require features gating the use of all their inner types, because of the following experience: In v0.48, the `windows` crate had `GlobalFree()` in the module `windows::Win32::System::Memory`, but v0.52 in `windows::Win32::Foundation`. When the impl only had the `Win32_Foundation` feature and the user didn't need `GlobalFree()`, but another free-function also gated with `Win32_Foundation`, there would be an unnecessary error about an incorrectly located function.

impl_with_acq_and_star!(
    "HANDLE_CloseHandle",
    windows::Win32::Foundation::HANDLE,
    with_acq_and_close_handle,
    with_mut_acq_and_close_handle,
    |handle| {
        let _ = unsafe { windows::Win32::Foundation::CloseHandle(handle) };
    }
);

impl_with_acq_and_star!(
    "HBITMAP_DeleteObject",
    windows::Win32::Graphics::Gdi::HBITMAP,
    with_acq_and_delete_object,
    with_mut_acq_and_delete_object,
    |h_bitmap| {
        unsafe { windows::Win32::Graphics::Gdi::DeleteObject(h_bitmap) };
    }
);

impl_with_acq_and_star!(
    "HBRUSH_DeleteObject",
    windows::Win32::Graphics::Gdi::HBRUSH,
    with_acq_and_delete_object,
    with_mut_acq_and_delete_object,
    |h_brush| {
        unsafe { windows::Win32::Graphics::Gdi::DeleteObject(h_brush) };
    }
);

impl_with_acq_and_star!(
    "HDC_DeleteDC",
    windows::Win32::Graphics::Gdi::HDC,
    with_acq_and_delete_dc,
    with_mut_acq_and_delete_dc,
    |h_dc| {
        unsafe { windows::Win32::Graphics::Gdi::DeleteDC(h_dc) };
    }
);

impl_with_acq_and_star!(
    "HFONT_DeleteObject",
    windows::Win32::Graphics::Gdi::HFONT,
    with_acq_and_delete_object,
    with_mut_acq_and_delete_object,
    |h_font| {
        unsafe { windows::Win32::Graphics::Gdi::DeleteObject(h_font) };
    }
);

impl_with_acq_and_star!(
    "HGDIOBJ_DeleteObject",
    windows::Win32::Graphics::Gdi::HGDIOBJ,
    with_acq_and_delete_object,
    with_mut_acq_and_delete_object,
    |h_gdi_obj| {
        unsafe { windows::Win32::Graphics::Gdi::DeleteObject(h_gdi_obj) };
    }
);

impl_with_acq_and_star!(
    "HGLOBAL_GlobalFree",
    windows::Win32::Foundation::HGLOBAL,
    with_acq_and_global_free,
    with_mut_acq_and_global_free,
    |h_global| {
        let _ = unsafe { windows::Win32::Foundation::GlobalFree(h_global) };
    }
);

impl_with_acq_and_star!(
    "HICON_DestroyIcon",
    windows::Win32::UI::WindowsAndMessaging::HICON,
    with_acq_and_destroy_icon,
    with_mut_acq_and_destroy_icon,
    |h_icon| {
        let _ = unsafe { windows::Win32::UI::WindowsAndMessaging::DestroyIcon(h_icon) };
    }
);

impl_with_acq_and_star!(
    "HLOCAL_LocalFree",
    windows::Win32::Foundation::HLOCAL,
    with_acq_and_local_free,
    with_mut_acq_and_local_free,
    |h_local| {
        let _ = unsafe { windows::Win32::Foundation::LocalFree(h_local) };
    }
);

impl_with_acq_and_star!(
    "HMENU_DestroyMenu",
    windows::Win32::UI::WindowsAndMessaging::HMENU,
    with_acq_and_destroy_menu,
    with_mut_acq_and_destroy_menu,
    |h_menu| {
        let _ = unsafe { windows::Win32::UI::WindowsAndMessaging::DestroyMenu(h_menu) };
    }
);

impl_with_acq_and_star!(
    "HMODULE_FreeLibrary",
    windows::Win32::Foundation::HMODULE,
    with_acq_and_free_library,
    with_mut_acq_and_free_library,
    |h_module| {
        let _ = unsafe { windows::Win32::Foundation::FreeLibrary(h_module) };
    }
);

impl_with_acq_and_star!(
    "HPALETTE_DeleteObject",
    windows::Win32::Graphics::Gdi::HPALETTE,
    with_acq_and_delete_object,
    with_mut_acq_and_delete_object,
    |h_palette| {
        unsafe { windows::Win32::Graphics::Gdi::DeleteObject(h_palette) };
    }
);

impl_with_acq_and_star!(
    "HPEN_DeleteObject",
    windows::Win32::Graphics::Gdi::HPEN,
    with_acq_and_delete_object,
    with_mut_acq_and_delete_object,
    |h_pen| {
        unsafe { windows::Win32::Graphics::Gdi::DeleteObject(h_pen) };
    }
);

impl_with_acq_and_star!(
    "HRGN_DeleteObject",
    windows::Win32::Graphics::Gdi::HRGN,
    with_acq_and_delete_object,
    with_mut_acq_and_delete_object,
    |h_rgn| {
        unsafe { windows::Win32::Graphics::Gdi::DeleteObject(h_rgn) };
    }
);

impl<R, F> Deref for ResGuard<R, F>
where
    F: FnOnce(R),
{
    type Target = R;

    fn deref(&self) -> &Self::Target {
        self.resource.as_ref().unwrap()
    }
}

impl<R, F> Drop for ResGuard<R, F>
where
    F: FnOnce(R),
{
    fn drop(&mut self) {
        self.free.take().unwrap()(self.resource.take().unwrap());
    }
}

#[cfg(test)]
mod tests {
    use windows::{
        core::PCWSTR,
        Win32::{
            Foundation::CloseHandle,
            Storage::FileSystem::{ReadFile, WriteFile},
            System::{
                Pipes::CreatePipe,
                Threading::{CreateEventW, SetEvent},
            },
        },
    };

    use super::ResGuard;

    #[test]
    fn new() {
        let event_handle = unsafe { CreateEventW(None, true, false, PCWSTR::null()) }
            .expect("should be able to create event handle");
        let event_handle = ResGuard::new(event_handle, |handle| {
            let _ = unsafe { CloseHandle(handle) };
        });

        assert_eq!(unsafe { SetEvent(*event_handle) }, Ok(()));
    }

    #[test]
    #[cfg(feature = "HANDLE_CloseHandle")]
    fn with_acq_and_close_handle() {
        let event_handle = ResGuard::with_acq_and_close_handle(|| unsafe {
            CreateEventW(None, true, false, PCWSTR::null())
        })
        .expect("should be able to create event handle");

        assert_eq!(unsafe { SetEvent(*event_handle) }, Ok(()));
    }

    #[test]
    fn with_mut_acquisition() {
        // Acquire pipe handles.
        let pipe_handles = ResGuard::with_mut_acquisition(
            |(read_handle, write_handle)| unsafe { CreatePipe(read_handle, write_handle, None, 0) },
            |(read_handle, write_handle)| {
                let _ = unsafe { CloseHandle(read_handle) };
                let _ = unsafe { CloseHandle(write_handle) };
            },
        )
        .expect("should be able to create pipe handles");
        let (read_handle, write_handle) = *pipe_handles;

        // Write.
        let bytes = [123, 45, 67];
        let mut bytes_written = 0;
        assert_eq!(
            unsafe { WriteFile(write_handle, Some(&bytes), Some(&mut bytes_written), None,) },
            Ok(())
        );
        assert_eq!(bytes_written as usize, bytes.len());

        // Read.
        let mut buffer = Vec::new();
        buffer.resize(bytes.len(), 0);
        let mut bytes_read = 0;
        assert_eq!(
            unsafe { ReadFile(read_handle, Some(&mut buffer), Some(&mut bytes_read), None) },
            Ok(())
        );
        assert_eq!(bytes_read as usize, buffer.len());
        assert_eq!(buffer, bytes);
    }
}