-
Notifications
You must be signed in to change notification settings - Fork 13.4k
from_ref, from_mut: clarify documentation #125897
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
Changes from 1 commit
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 |
---|---|---|
|
@@ -777,8 +777,8 @@ where | |
|
||
/// Convert a reference to a raw pointer. | ||
/// | ||
/// This is equivalent to `r as *const T`, but is a bit safer since it will never silently change | ||
/// type or mutability, in particular if the code is refactored. | ||
/// For `r: &T`, `from_ref(r)` is equivalent to `r as *const T`, but is a bit safer since it will | ||
/// never silently change type or mutability, in particular if the code is refactored. | ||
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. In addition, I think it is worth also clarifying that for However, https://doc.rust-lang.org/reference/type-coercions.html#coercion-types does seem to say that when 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.
Actually, 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 don't think this is the right place to discuss the semantics of coercions. They behave the same everywhere, surely we don't want to explain them in each function that takes a reference. (e.g. I also don't see why this PR should be blocked on #56604. That issue is long-standing so I don't expect it to be resolved soon, this clarification however is necessary short-term according to yourself as you said it is ambiguous.
IMO the new wording is not necessary under any ruleset, since 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. It's analogous to the situation for
In both cases, the construct is designed to make things safer, but not understanding the interaction with coercions is especially dangerous. 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 suggest adding the following text, which really gets to the point: For |
||
#[inline(always)] | ||
#[must_use] | ||
#[stable(feature = "ptr_from_ref", since = "1.76.0")] | ||
|
@@ -791,8 +791,8 @@ pub const fn from_ref<T: ?Sized>(r: &T) -> *const T { | |
|
||
/// Convert a mutable reference to a raw pointer. | ||
/// | ||
/// This is equivalent to `r as *mut T`, but is a bit safer since it will never silently change | ||
/// type or mutability, in particular if the code is refactored. | ||
/// For `r: &mut T`, `from_mut(r)` is equivalent to `r as *mut T`, but is a bit safer since it will | ||
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. Same here: scale back the "is equivalent to" claim. |
||
/// never silently change type or mutability, in particular if the code is refactored. | ||
#[inline(always)] | ||
#[must_use] | ||
#[stable(feature = "ptr_from_ref", since = "1.76.0")] | ||
|
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.
We shouldn't be leading off with this incorrect statement. I suggest we replcae "is equivalent" is "is often but not always equivalent".