wio-rs icon indicating copy to clipboard operation
wio-rs copied to clipboard

Provide easy way to retain-construct from a reference

Open Manishearth opened this issue 4 years ago • 4 comments

Currently ComPtr::from_raw adopts the given pointer, which means that it assumes that it has already been addreffed.

This isn't always true, though, sometimes you want to retain the refcount of the old pointer, which you're simply borrowing from to create a new reference. We should have a separate constructor for this, the current way of doing it is to mem::forget(ptr.clone()) which is clunky.

Manishearth avatar Aug 13 '19 18:08 Manishearth

Something like ComPtr::clone_from_raw?

retep998 avatar Aug 13 '19 19:08 retep998

Yep!

Manishearth avatar Aug 13 '19 19:08 Manishearth

@retep998 @Manishearth How about manully call Release instead of impl Drop?
The main reason for i use ComPtr just want to wrap the raw pointer to call it's method.. use Deref to instead (ptr -> m() in C ),

if need change RefCount, we could call clone() or Release() explicitly

    pub unsafe fn from_fn<F>(fun: F) -> Result<ComPtr<T>, HRESULT>
    where
        T: Interface,
        F: FnOnce(*mut *mut T) -> HRESULT,
{}

// I had change the signature like this, but still not ideal.
// I think just need new()/from_raw(), to Construct,

This is common to store pointer in WndExtraMemtory, and to retrieve, as raw pointer, ComPtr::new(p), deal something, then leave without Release...

mildcwen avatar Jan 20 '22 06:01 mildcwen

I mean yes, there are many ways to make it work, but in this case adopt/retain semantics are a common difference and I think it would be good for documentation, even, to have both as first class APIs that are well documented.

Manishearth avatar Jan 20 '22 16:01 Manishearth