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 as_{,mut_}ptr methods to Option #80308

Closed
wants to merge 2 commits into from
Closed

Conversation

nvzqz
Copy link
Contributor

@nvzqz nvzqz commented Dec 22, 2020

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.

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.
@rust-highfive
Copy link
Collaborator

r? @sfackler

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 22, 2020
@nvzqz
Copy link
Contributor Author

nvzqz commented Dec 22, 2020

@rustbot modify labels +T-libs

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Dec 22, 2020
@nvzqz
Copy link
Contributor Author

nvzqz commented Jan 1, 2021

@rustbot modify labels +A-result-option

@rustbot rustbot added the A-result-option Area: Result and Option combinators label Jan 1, 2021
@m-ou-se m-ou-se assigned m-ou-se and unassigned sfackler Jan 6, 2021
Copy link
Member

@m-ou-se m-ou-se left a 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`.
Copy link
Member

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?

Copy link
Contributor Author

@nvzqz nvzqz Jan 6, 2021

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.

Copy link
Member

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. ^^

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

@@ -985,6 +986,83 @@ impl<T> Option<T> {
}
}

impl<T> Option<&T> {
/// Converts from `Option<&T>` (or `&Option<&T>`) to `*const T`.
Copy link
Member

@m-ou-se m-ou-se Jan 6, 2021

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 Option<&T> by value instead? Or I suppose that might be problematic with the &mut versions? Edit: You already mentioned that. Never mind. :)

Regardless, it's probably better to leave out the (or `&Option<&T>`) part from the documentation, as that just might add confusion.

Copy link
Contributor Author

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")]
Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

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:

// 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.

Copy link
Contributor Author

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.

@m-ou-se
Copy link
Member

m-ou-se commented Jan 6, 2021

One more thought: It might be good to explicitly mention that None becomes a null pointer. As it is now, that might not be obvious without checking the implementation or the documentation of pointer::as_ref.

@m-ou-se m-ou-se added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 15, 2021
@JohnCSimon
Copy link
Member

ping from triage:
@nvzqz can you post your update on this PR? Thanks

@Dylan-DPC-zz
Copy link

@nvzqz
thanks for taking the time to contribute. I have to close this due to inactivity. If you wish and you have the time you can open a new PR with these changes and we'll take it from there. Thanks

@Dylan-DPC-zz Dylan-DPC-zz added S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-result-option Area: Result and Option combinators S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants