-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Add as_{,mut_}ptr methods to Option
#80308
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
Conversation
These enable simpler conversion from `Option<&{,mut} T>` to `*const T`
and `Option<&mut T>` to `*mut T`.
These operations are the opposite of `<*const T>::as_ref` and
`<*mut T>::as_mut`.
In FFI, the types `Option<&T>` and `Option<&mut T>` are semantically
equivalent to `*const T` and `*mut T`, and they can even be safely used
in place of those types.
The `self` type in each method is a reference because `Option<&mut T>`
does not implement `Copy`. These methods would otherwise move `self`,
making them inconvenient or sometimes impossible to use.
|
r? @sfackler (rust-highfive has picked a reviewer for you, use r? to override) |
|
@rustbot modify labels +T-libs |
|
@rustbot modify labels +A-result-option |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Looks useful.
Can you open a tracking issue for this?
| impl<T> Option<&T> { | ||
| /// Converts from `Option<&T>` (or `&Option<&T>`) to `*const T`. | ||
| /// | ||
| /// This is the opposite of `<*const T>::as_ref`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you turn <*const T>::as_ref into a link to its documentation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing. Do you happen to know how to intra-doc link pointer type methods? Otherwise I'll just use the full URL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Paging @jyn514, who probably knows this. ^^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[..][pointer::as_ref] seems to work, but that doesn't give you control over which of the two as_refs it links to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it guaranteed that #method.as_ref and #method.as_ref-1 will refer to the const and mut variants respectively? If not, it might be worth adding a custom id.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pointer was recently implemented and isn't in beta yet, so you can't use it until the beta bump in 6 weeks (or else doc --stage 0 will break). #80181 (comment)
There is currently no way to distinguish *const from *mut, I wouldn't consider impl-1 stable and would prefer not to use it since the link checker won't warn if *const and *mut get swapped.
| } | ||
|
|
||
| impl<T> Option<&T> { | ||
| /// Converts from `Option<&T>` (or `&Option<&T>`) to `*const T`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(or
&Option<&T>)
Maybe this should take an Edit: You already mentioned that. Never mind. :)Option<&T> by value instead? Or I suppose that might be problematic with the &mut versions?
Regardless, it's probably better to leave out the (or `&Option<&T>`) part from the documentation, as that just might add confusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added that for consistency with the docs of Option::as_deref and Option::as_deref_mut. I agree it doesn't add much value.
| /// assert_eq!(x, unsafe { *ptr }); | ||
| /// ``` | ||
| #[unstable(feature = "option_as_ptr", issue = "none")] | ||
| #[rustc_const_unstable(feature = "option_as_ptr", issue = "none")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason you put #[rustc_const_unstable] here, but not on the one above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because these are over &mut T, which is unstable in const fn. Whereas the one above is &T, which has no issues in const fn. Should they both be #[rustc_const_unstable] regardless?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what the state of that attribute is. In the rustc source code, it says:
rust/compiler/rustc_mir/src/transform/check_consts/mod.rs
Lines 113 to 116 in 8fec6c7
| // Functions with `#[unstable]` are const-unstable. | |
| // | |
| // FIXME(ecstaticmorse): We should keep const-stability attributes wholly separate from normal stability | |
| // attributes. `#[unstable]` should be irrelevant. |
Which doesn't really give an answer here.
I suppose it's useful to remind us we're also stabilizing the constness of the function at the point when we stabilize these new functions. So it wouldn't hurt to add it to the first one too, just in case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a fair point. I'll make this change then.
|
One more thought: It might be good to explicitly mention that |
|
ping from triage: |
|
@nvzqz |
These enable simpler conversion from
Option<&{,mut} T>to*const TandOption<&mut T>to*mut T.These operations are the opposite of
<*const T>::as_refand<*mut T>::as_mut.In FFI, the types
Option<&T>andOption<&mut T>are semantically equivalent to*const Tand*mut T, and they can even be safely used in place of those types.The
selftype in each method is a reference becauseOption<&mut T>does not implementCopy. These methods would otherwise moveself, making them inconvenient or sometimes impossible to use.