Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UniquePtr::as_mut_ptr #892

Merged
merged 2 commits into from
Dec 25, 2024
Merged

Conversation

adetaylor
Copy link
Collaborator

cxx accepts functions that take *mut T as an argument.
Such functions are of course used under odd niche circumstances;
it would be safer and better to take a &mut Pin.

But in cases where such a function does exist, we'd most commonly
want to provide it a mutable pointer to a T which is safely
and uniquely owned by Rust, within a UniquePtr.

It was previously quite hard to do this:

    let mut a = /* make UniquePtr to a thing */
    unsafe { ffi::TakeA(std::pin::Pin::<&mut ffi::A>::into_inner_unchecked(a.pin_mut())) };

With this extra API, it becomes much more ergonomic:

    let mut a = /* make UniquePtr to a thing */
    unsafe { ffi::TakeA(a.as_mut_ptr()) }

Inspired by google/autocxx#558

cxx accepts functions that take *mut T as an argument.
Such functions are of course used under odd niche circumstances;
it would be safer and better to take a &mut Pin<T>.

But in cases where such a function does exist, we'd most commonly
want to provide it a mutable pointer to a T which is safely
and uniquely owned by Rust, within a UniquePtr.

It was previously quite hard to do this:

    let mut a = /* make UniquePtr to a thing */
    unsafe { ffi::TakeA(std::pin::Pin::<&mut ffi::A>::into_inner_unchecked(a.pin_mut())) };

With this extra API, it becomes much more ergonomic:

    let mut a = /* make UniquePtr to a thing */
    unsafe { ffi::TakeA(a.as_mut_ptr()) }
Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable to me. However are you sure you want as_mut_ptr unsafe to call? It seems 100% equivalent to u.as_ptr() as *mut _ which is safe code.

@adetaylor
Copy link
Collaborator Author

Thanks. I wasn't quite sure. On the one hand, manipulating the returned pointer could break the contents of the UniquePtr (so it's unsafe in that sense) but on the other hand, such unsafety is restricted to the contents of the UniquePtr and that's wild-west C++ land anyway.

I'll revise the PR to remove the unsafe. Thanks.

@dtolnay
Copy link
Owner

dtolnay commented Jun 29, 2021

You can also remove the exclusive reference to self and use a shared reference. Getting the pointer from a unique_ptr doesn't require that the caller has exclusive access to the unique_ptr.

@theomonnom
Copy link

theomonnom commented Sep 18, 2022

Hey, any updates to this to make it merge?

@dtolnay dtolnay merged commit e580f4c into dtolnay:master Dec 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants