Skip to content

Commit ab01522

Browse files
authored
Remove Drop impl on VARIANT and PROPVARIANT (#3886)
1 parent 3ddd8f7 commit ab01522

5 files changed

Lines changed: 58 additions & 20 deletions

File tree

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
/// Custom code to free a handle.
1+
/// Custom code to free a resource.
22
///
3-
/// This is similar to the [`Drop`] trait, and may be used to implement [`Drop`], but allows handles
4-
/// to be dropped depending on context.
3+
/// This is similar to the [`Drop`] trait, and may be used to implement [`Drop`], but allows resources
4+
/// to be freed depending on context.
55
pub trait Free : Clone {
6-
/// Calls the handle's free function.
6+
/// Calls the resource's free function.
77
///
88
/// # Safety
9-
/// The handle must be owned by the caller and safe to free.
9+
/// The resource must be owned by the caller and safe to free.
1010
unsafe fn free(&mut self);
1111
}
1212

13-
/// A wrapper to provide ownership for handles to automatically drop via the handle's [`Free`] trait.
13+
/// A wrapper to provide ownership for resources to automatically drop via the resource's [`Free`] trait.
1414
#[repr(transparent)]
1515
#[derive(PartialEq, Eq, Default, Debug)]
1616
pub struct Owned<T: Free>(T);
1717

1818
impl<T: Free> Owned<T> {
19-
/// Takes ownership of the handle.
19+
/// Takes ownership of the resource.
2020
///
2121
/// # Safety
2222
///
23-
/// The handle must be owned by the caller and safe to free.
23+
/// The resource must be owned by the caller and safe to free.
2424
pub unsafe fn new(x: T) -> Self {
2525
Self(x)
2626
}
2727

28-
/// Consumes the `Owned<T>` and relinquishes ownership of the handle.
29-
/// The caller is now responsible for freeing the returned handle.
30-
#[must_use = "losing the handle will leak it"]
28+
/// Consumes the `Owned<T>` and relinquishes ownership of the resource.
29+
/// The caller is now responsible for freeing the returned resource.
30+
#[must_use = "losing the resource will leak it"]
3131
pub fn into_raw(o: Self) -> T {
3232
let o = core::mem::ManuallyDrop::new(o);
3333
o.0.clone()

crates/libs/core/src/windows.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ mod event;
99
#[cfg(feature = "std")]
1010
pub use event::*;
1111

12-
mod handles;
13-
pub use handles::*;
12+
mod resources;
13+
pub use resources::*;
1414

1515
pub use windows_strings::*;
1616

crates/libs/windows/src/extensions/Win32/System/StructuredStorage.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ impl Clone for PROPVARIANT {
2929
}
3030
}
3131

32-
impl Drop for PROPVARIANT {
33-
fn drop(&mut self) {
34-
unsafe { _ = PropVariantClear(self) };
32+
impl Free for PROPVARIANT {
33+
unsafe fn free(&mut self) {
34+
_ = PropVariantClear(self);
3535
}
3636
}
3737

crates/libs/windows/src/extensions/Win32/System/Variant.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ impl Clone for VARIANT {
3030
}
3131
}
3232

33-
impl Drop for VARIANT {
34-
fn drop(&mut self) {
35-
unsafe { _ = VariantClear(self) };
33+
impl Free for VARIANT {
34+
unsafe fn free(&mut self) {
35+
_ = VariantClear(self);
3636
}
3737
}
38+
3839
impl VARIANT {
3940
pub fn vt(&self) -> VARENUM {
4041
unsafe { self.Anonymous.Anonymous.vt }

crates/tests/misc/variant/tests/tests.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use std::rc::Rc;
12
use windows::Foundation::Uri;
23
use windows::Win32::Foundation::{E_INVALIDARG, TYPE_E_TYPEMISMATCH};
3-
use windows::Win32::System::Com;
44
use windows::Win32::System::Com::StructuredStorage::PROPVARIANT;
5+
use windows::Win32::System::Com::{self, IAgileObject, IAgileObject_Impl};
56
use windows::Win32::System::Variant::*;
67
use windows_core::*;
78

@@ -265,3 +266,39 @@ fn test_propvariant() -> Result<()> {
265266

266267
Ok(())
267268
}
269+
270+
#[implement(IAgileObject)]
271+
struct Canary {
272+
_counter: Rc<()>,
273+
}
274+
impl IAgileObject_Impl for Canary_Impl {}
275+
276+
#[test]
277+
fn test_owned_variant() -> Result<()> {
278+
let counter = Rc::new(());
279+
let unk: IUnknown = Canary {
280+
_counter: counter.clone(),
281+
}
282+
.into();
283+
assert_eq!(Rc::strong_count(&counter), 2);
284+
{
285+
let _owned = unsafe { Owned::new(VARIANT::from(unk)) };
286+
}
287+
assert_eq!(Rc::strong_count(&counter), 1);
288+
Ok(())
289+
}
290+
291+
#[test]
292+
fn test_owned_propvariant() -> Result<()> {
293+
let counter = Rc::new(());
294+
let unk: IUnknown = Canary {
295+
_counter: counter.clone(),
296+
}
297+
.into();
298+
assert_eq!(Rc::strong_count(&counter), 2);
299+
{
300+
let _owned = unsafe { Owned::new(PROPVARIANT::from(unk)) };
301+
}
302+
assert_eq!(Rc::strong_count(&counter), 1);
303+
Ok(())
304+
}

0 commit comments

Comments
 (0)