-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
mem::discriminant() #1696
mem::discriminant() #1696
Conversation
Is there anything left to resolve here? The discussion on the PR seems to have reached a conclusion already. |
Remove the With specialization (I'm not sure how to do it, but): fn type_id<T: 'static>(t: &T) TypeId { panic!() }
/* specialize */ fn type_id<T: Any>(t: &T) -> TypeId { t.get_type_id() } Now you can get the |
@sfackler there has been a demand for a RFC, which is a pretty good indication that one is necessary, even if there’s already some consensus. |
I removed the |
cc @rust-lang/libs because it is technically a libs stuff and also @rust-lang/lang (especially wrt Reflect). |
Sigh.. @ubsan The point, as discussed on the other thread, is that specialization is not yet stable. The (For completeness's sake - I think the |
@glaebhoerl There is no desire to maintain parametricity among the vast majority of Rust users. And making someone's day worse is a good reason to reason to get rid of this idea of "maintaining parametricity". |
I agree. |
One thing that may not be obvious: you can opt out of |
Hmm. While I agree that we are unlikely to roll back specialization, I do feel mildly uncomfortable about removing the |
Besides the
|
@nikomatsakis What speaks Not to mention, to use this function stably would require people to have an |
To have an |
@ubsan it seems clear that it's a kind of reflection, is all -- that said, not one greater in magnitude than |
@durka I could imagine someone implementing something, where they wanted to check quickly if two variants were the same. This is a useful small function, if you're doing it a lot for a lot of enums (for example, implementing fn enum_quick_eq_check<T: Any>(t1: T, t2: T) -> bool {
std::mem::discriminant(t1) == std::mem::discriminant(t2)
} With the current bounds, you must write Now, imagine you have an enum like this: enum Foo<'a> {
...
} You can't use (of course, one could always implement a macro, but...) |
Note that Reflect and Any are automatically implemented ... I think you On Aug 2, 2016 5:52 PM, "Nicole Mazzuca" [email protected] wrote:
|
Any requires 'static. On Aug 3, 2016 21:52, "Niko Matsakis" [email protected] wrote:
|
I could imagine |
Ah, so the problem is that I'm shifting my opinion slightly. It does seem like it'd be nice to have a separate RFC that talks about I really don't see us backing away from specialization unless there is some horrible flaw that makes the design incoherent, and even then I might imagine us trying to come up with some alternative that achieves similar goals. |
(I mean, by accepting specialization we already decided the intent to drop parametricity, clearly.) |
Basically what I had in the back of my mind is that, as far as I've seen, Haskellers are almost to a person surprised and disappointed that Rust is dropping parametricity, but there's this self-reinforcing sorting effect at work whereby they don't feel like it's worth getting involved with Rust, because they feel like it doesn't share their values/priorities, and so the Rust community tends to be populated by those people who already don't care about it or aren't bothered by it. But what if at some point someone does decide to involve themselves, and makes the case more persuasively than I could. Just in case. (For what it's worth -- I do think |
After using specialization in C++ for years I was similarly surprised by people defending parametricity. "Are these people mad?" - asked I myself - "Why does this abstract property have any value for them? What useful practical guarantees does it give? What guarantees can it give at all, given that it couldn't save dropck, the only thing it was used for?" After reading dozens and dozens of pages of discussion following RFC 1210 I still ask myself these questions... Okay, this is an offtopic, this PR is for discussing |
(as I was saying - this isn't the best place to ask those questions) |
Nominated. |
Was it discussed at the meeting? |
@durka It was, and will be entering FCP as soon as the team has a moment to write up the summary for the FCP comment (likely tomorrow). |
On a related note, we decided to put the |
Hear ye, hear ye! This RFC is now entering final comment period. The @rust-lang/lang team discussed it and we are inclined to accept. Major points of discussion follow (this also includes comments from the PR):
@rust-lang/lang and @rust-lang/libs members, please check off your name to signal agreement. Leave a comment with concerns or objections. Others, please leave comments. Thanks!
|
@rust-lang/libs concluded today we approve as a team going into FCP for merging. |
My concern is that my application needs the hinted-at |
I guess I should state my opinions on the unresolved questions:
|
@briansmith the problem is we have no way to deal with the repr types, in the type system. We must have the |
If rust-lang/rust#8995 is implemented, it would be possible to just make |
@briansmith do you plan to invoke this on arbitrary enum types that are out of your control? or would a derive-based sol'n be adequate. I agree it'd be nice to expose the |
In my case, I define the enums and I write the code that uses them. See
Basically, I'm just looking for a safer version of |
This has now been in FCP for 4 weeks. |
What remains to be done before this can be closed? Just curious, since the teams post says inclined to accept, but that was a long time ago. |
@durka Whelp, my turn to apologize for the delay yet again. Between RustConf and the shift away from meetings (but without a dashboard just yet), the teams have fallen behind on administrative work. None of the commentary during FCP changes this RFC in a deep way -- there are some possible extensions we may want to pursue, but these need not block landing the current proposal as a starting point. Given that the lang and libs teams are also in agreement, I'm going to merge. |
I'll update the PR and create a tracking issue later today. On Thu, Sep 15, 2016 at 11:53 AM, Aaron Turon [email protected]
|
It just occurred to me that |
How could I have the discriminant value without create the real variant ? Example: use std::collections::HashMap;
use std::mem::discriminant;
use std::net::Ipv4Addr;
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub enum GenericChunk<'a> {
IpProtocolFamily(u8),
Ipv4SourceAddr(Ipv4Addr),
AuthenticationKey(&'a [u8]),
}
fn main() {
let mut chunks = HashMap::new();
chunks.insert(
discriminant(&GenericChunk::IpProtocolFamily(0)),
GenericChunk::IpProtocolFamily(42),
);
assert_eq!(
chunks.get(&discriminant(&GenericChunk::IpProtocolFamily(0))),
Some(&GenericChunk::IpProtocolFamily(42))
);
assert_eq!(
chunks.get(&discriminant(&GenericChunk::AuthenticationKey(b"" as _))),
None
);
} Here, I'm force to create a temporary value in order to have the discriminant, even if discriminant don't really need it. |
The
discriminant_value
intrinsic won't be stabilized anytime soon in its current form because it leaks too much memory layout information. The wrapper proposed here returns an opaque newtype to allow for information hiding.cc rust-lang/rust#24263 rust-lang/rust#34785 @ubsan @sfackler
Rendered