-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
[RFC accepted] add wrapper for discriminant_value intrinsic #34785
Conversation
38a41cb
to
609c17e
Compare
I just realized the wrapper should be safe. Another commit incoming... |
pub struct TraitObject { | ||
pub data: *mut (), | ||
pub vtable: *mut (), | ||
} | ||
|
||
/// Returns the value of the discriminant for the enum variant in `v`. |
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.
It is probably worth adding a warning that discriminants may not compare in the correct order if the enum is #[repr(i64)]
due to overflow.
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.
Will do.
On Tue, Jul 12, 2016 at 1:43 PM, Steven Fackler notifications@github.com
wrote:
In src/libcore/raw.rs
#34785 (comment):pub struct TraitObject {
pub data: *mut (),
pub vtable: *mut (),
}
+
+/// Returns the value of the discriminant for the enum variant inv
.It is probably worth adding a warning that discriminants may not compare
in the correct order if the enum is #[repr(u64)] due to overflow.—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/rust-lang/rust/pull/34785/files/609c17e8d0ceac437261df2c6b4231225f1faf08#r70485187,
or mute the thread
https://github.com/notifications/unsubscribe/AAC3n4IaNbDuCLcSiYjaKILI3yho7yvLks5qU9IrgaJpZM4JKnnt
.
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.
Wait, I don't understand. You mean if the enum is not #[repr(u64)]
?
On Tue, Jul 12, 2016 at 1:46 PM, Alex Burka durka42@gmail.com wrote:
Will do.
On Tue, Jul 12, 2016 at 1:43 PM, Steven Fackler notifications@github.com
wrote:In src/libcore/raw.rs
#34785 (comment):pub struct TraitObject {
pub data: *mut (),
pub vtable: *mut (),
}
+
+/// Returns the value of the discriminant for the enum variant inv
.It is probably worth adding a warning that discriminants may not compare
in the correct order if the enum is #[repr(u64)] due to overflow.—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/rust-lang/rust/pull/34785/files/609c17e8d0ceac437261df2c6b4231225f1faf08#r70485187,
or mute the thread
https://github.com/notifications/unsubscribe/AAC3n4IaNbDuCLcSiYjaKILI3yho7yvLks5qU9IrgaJpZM4JKnnt
.
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.
Er, yeah. Your doc change is what I was thinking of.
I might personally prefer this being located in the Also, could the actual stabilization be deferred to a later date? The libs team tends to not apply stabilizations mid-cycle but instead places issues into final comment period at the end of which we decide on the stabilization outcome (as a result of the discussion, if any, that happened). cc @rust-lang/libs, any objections on moving this to FCP though? Seems good to me! |
OK, I had previously asked at the tracking issue if this should be insta-stable (since it is basically stabilizing the intrinsic, even though it doesn't actually do that) and @brson said yes. But I'm happy to mark it unstable for now. I'll move it to |
29e3b0d
to
1001205
Compare
@sfackler's comment was hidden, but I added a note about comparisons. |
eb26969
to
3a88d88
Compare
/// enum Foo { A(String), B(i32) } | ||
/// | ||
/// # fn main() { | ||
/// assert!(mem::discriminant(Foo::A("bar")) != mem::discriminant(Foo::A("baz"))); |
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 think these examples may not compile, the discriminants should be the same here, right? Additionally, shouldn't a shared reference be passed in?
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.
Yeah, I just noticed that and fixed it.
On Tue, Jul 12, 2016 at 2:19 PM, Alex Crichton notifications@github.com
wrote:
In src/libcore/mem.rs
#34785 (comment):+/// same order as actual enum variants would under
#[derive(PartialOrd)]
.
+///
+/// IfT
is not an enum, the return value is unspecified.
+///
+/// # Example
+///
+/// This can be used to compare enums that carry data, while disregarding
+/// the actual data:
+///
+/// ```
+/// use std::mem;
+///
+/// enum Foo { A(String), B(i32) }
+///
+/// # fn main() {
+/// assert!(mem::discriminant(Foo::A("bar")) != mem::discriminant(Foo::A("baz")));I think these examples may not compile, the discriminants should be the
same here, right? Additionally, shouldn't a shared reference be passed in?—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/rust-lang/rust/pull/34785/files/3a88d8811ae23af836e8ed4349e4a96656303878#r70492383,
or mute the thread
https://github.com/notifications/unsubscribe/AAC3n1nIqX5ydVTZ2ue3rNp94IPEA-khks5qU9qggaJpZM4JKnnt
.
987979d
to
c35ec62
Compare
@alexcrichton I want to stabilize type_name or something like it, so we might want to keep std::raw around for things like that. |
Is it the case that a discriminant can never be larger than |
For the record (on the theme of my previous comment) I still think it would be better to use the type system to make this function's return type better. But I also understand we want to stabilize something. |
@sfackler hm that's a good point about |
c35ec62
to
90e4530
Compare
I wasn't initially a fan of |
assert_eq!(mem::discriminant(&NullablePointer::Something(&CONST)), 0); | ||
|
||
// unlike the test for the intrinsic, there are no tests with non-enums here | ||
// because the result is documented to be unspecified |
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.
You could still call it on a struct, just to check that it doesn't panic.
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.
Done.
RFC posted at rust-lang/rfcs#1696. |
☔ The latest upstream changes (presumably #35906) made this pull request unmergeable. Please resolve the merge conflicts. |
Waiting to rebase until the RFC process concludes. |
a7de3ad
to
a8c3cf6
Compare
Rebased and updated. I removed the I updated the stability section of the documentation to say (again, nobody opined during FCP):
|
@nagisa Can you take over this review, given that you've already looked at the PR? |
/// | ||
/// # Stability | ||
/// | ||
/// Discriminants can change if enum variants are reordered, if a new variant is added |
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.
As the return value of this wrapper is a opaque value Discriminant<T>
, this section reads weird. Namely it changing makes little sense because you aren’t supposed to be able to inspect it directly. Rather the stability and behaviour should be described in terms of PartialEq
and Hash
.
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 good point.
#[unstable(feature = "discriminant_value", reason = "recently added, follows RFC", issue = "24263")] | ||
impl<T> fmt::Debug for Discriminant<T> { | ||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
self.0.fmt(fmt) |
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.
This probably wants to use a debug builder and output something along the lines of Discriminant(<number>)
, rather than outputting a plain number.
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.
OK.
pub struct Discriminant<T>(u64, PhantomData<*const T>); | ||
|
||
#[unstable(feature = "discriminant_value", reason = "recently added, follows RFC", issue = "24263")] | ||
impl<T> Copy for Discriminant<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.
Why not derive all these implementations?
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.
You'd get nonsense bounds on 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.
I thought I'd included a comment to this effect. I'll add one.
a8c3cf6
to
e447d42
Compare
e447d42
to
a84b550
Compare
I think it's ready, just waiting for tests. |
@durka OK, just give a ping once it's ready for bors and i'll send it along. |
Tests and tidy pass locally. |
Huzzah! @bors: r=nagisa |
@bors? you there? |
@bors: r+ |
@bors r=nagisa |
@bors: r=nagisa |
Should I try reopening as a new PR? |
Moved to #36823. |
add wrapper for discriminant_value, take 2 [This is #34785 reopened, since @bors apparently gave up on that thread.] add wrapper for discriminant_value intrinsic Implementation of [RFC 1696](https://github.com/rust-lang/rfcs/blob/master/text/1696-discriminant.md). Wraps the `discriminant_value` intrinsic under the name `std::mem::discriminant`. In order to avoid prematurely leaking information about the implementation of enums, the return value is an opaque type, generic over the enum type, which implements Copy, Clone, PartialEq, Eq, Hash, and Debug (notably not PartialOrd). There is currently no way to get the value out excepting printing the debug representation. The wrapper is safe and can be stabilized soon as per discussion in #24263. cc @aturon r? @nagisa
add wrapper for discriminant_value intrinsic
Wraps the
discriminant_value
intrinsic under the namestd::mem::discriminant
. In order to avoid prematurely leaking information about the implementation of enums, the return value is an opaque type, generic over the enum type, which implementsCopy
,Clone
,PartialEq
,Eq
,Hash
, andDebug
(notably notPartialOrd
). There is currently no way to get the value out excepting printing the debug representation.The wrapper is safe and can be stabilized soon as per discussion in #24263.
r? @brson