-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -152,6 +152,7 @@ use crate::pin::Pin; | |||||||||
use crate::{ | ||||||||||
convert, fmt, hint, mem, | ||||||||||
ops::{self, Deref, DerefMut}, | ||||||||||
ptr, | ||||||||||
}; | ||||||||||
|
||||||||||
/// The `Option` type. See [the module level documentation](self) for more. | ||||||||||
|
@@ -985,6 +986,83 @@ impl<T> Option<T> { | |||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Can you turn There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it guaranteed that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There is currently no way to distinguish *const from *mut, I wouldn't consider |
||||||||||
/// | ||||||||||
/// # Examples | ||||||||||
/// | ||||||||||
/// ``` | ||||||||||
/// #![feature(option_as_ptr)] | ||||||||||
/// | ||||||||||
/// let opt = Some(&3); | ||||||||||
/// let ptr = opt.as_ptr(); | ||||||||||
/// | ||||||||||
/// assert_eq!(opt, unsafe { ptr.as_ref() }); | ||||||||||
/// ``` | ||||||||||
#[unstable(feature = "option_as_ptr", issue = "none")] | ||||||||||
#[inline] | ||||||||||
pub const fn as_ptr(&self) -> *const T { | ||||||||||
match self { | ||||||||||
Some(x) => *x, | ||||||||||
None => ptr::null(), | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
impl<T> Option<&mut T> { | ||||||||||
/// Converts from `Option<&mut T>` (or `&Option<&mut T>`) to `*const T`. | ||||||||||
/// | ||||||||||
/// # Examples | ||||||||||
/// | ||||||||||
/// ``` | ||||||||||
/// #![feature(option_as_ptr)] | ||||||||||
/// | ||||||||||
/// let mut x = 3; | ||||||||||
/// let opt = Some(&mut x); | ||||||||||
/// let ptr = opt.as_ptr(); | ||||||||||
/// | ||||||||||
/// 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 commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason you put There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because these are over There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
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 commentThe reason will be displayed to describe this comment to others. Learn more. That's a fair point. I'll make this change then. |
||||||||||
#[inline] | ||||||||||
pub const fn as_ptr(&self) -> *const T { | ||||||||||
match self { | ||||||||||
Some(x) => *x, | ||||||||||
None => ptr::null(), | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
/// Converts from `Option<&mut T>` (or `&mut Option<&mut T>`) to `*mut T`. | ||||||||||
/// | ||||||||||
/// This is the opposite of `<*mut T>::as_mut`. | ||||||||||
/// | ||||||||||
/// # Examples | ||||||||||
/// | ||||||||||
/// ``` | ||||||||||
/// #![feature(option_as_ptr)] | ||||||||||
/// | ||||||||||
/// let mut x = 3; | ||||||||||
/// let mut opt = Some(&mut x); | ||||||||||
/// | ||||||||||
/// let ptr = opt.as_mut_ptr(); | ||||||||||
/// unsafe { *ptr = 4 }; | ||||||||||
/// | ||||||||||
/// assert_eq!(x, 4); | ||||||||||
/// ``` | ||||||||||
#[unstable(feature = "option_as_ptr", issue = "none")] | ||||||||||
#[rustc_const_unstable(feature = "option_as_ptr", issue = "none")] | ||||||||||
#[inline] | ||||||||||
pub const fn as_mut_ptr(&mut self) -> *mut T { | ||||||||||
match self { | ||||||||||
Some(x) => *x, | ||||||||||
None => ptr::null_mut(), | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
impl<T: Copy> Option<&T> { | ||||||||||
/// Maps an `Option<&T>` to an `Option<T>` by copying the contents of the | ||||||||||
/// 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.
Maybe this should take anEdit: 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
andOption::as_deref_mut
. I agree it doesn't add much value.