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
use super::*;
use core::ffi::c_void;
/// Revokes an event registration when dropped.
///
/// Returned by event-registration methods such as `Click` and `Closed`.
///
/// Use [`Self::into_token`] to manage revocation manually, or [`Self::forget`] to disable
/// automatic revocation without releasing the retained source interface.
#[must_use = "event registrations are revoked when the EventRevoker is dropped"]
pub struct EventRevoker {
source: IUnknown,
token: i64,
remove: unsafe extern "system" fn(*mut c_void, i64) -> HRESULT,
}
impl EventRevoker {
#[doc(hidden)]
pub fn new<I: Interface>(
source: I,
token: i64,
remove: unsafe extern "system" fn(*mut c_void, i64) -> HRESULT,
) -> Self {
let source = unsafe { IUnknown::from_raw(source.into_raw()) };
Self {
source,
token,
remove,
}
}
/// Returns the registration token without revoking the handler.
///
/// The caller is responsible for passing the token to the corresponding `Remove*` method
/// when the handler is no longer needed.
pub fn into_token(self) -> i64 {
let mut this = core::mem::ManuallyDrop::new(self);
let token = this.token;
// Release the source interface reference without calling Remove*.
unsafe { core::ptr::drop_in_place(&mut this.source) };
token
}
/// Disables automatic revocation without releasing the retained source interface.
pub fn forget(self) {
core::mem::forget(self);
}
}
impl Drop for EventRevoker {
fn drop(&mut self) {
// Best-effort: discard errors silently (Drop cannot return Result).
unsafe {
let _ = (self.remove)(self.source.as_raw(), self.token);
}
}
}