-
Notifications
You must be signed in to change notification settings - Fork 13.3k
add wrapper for discriminant_value, take 2 #36823
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 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 |
---|---|---|
|
@@ -15,7 +15,12 @@ | |
|
||
#![stable(feature = "rust1", since = "1.0.0")] | ||
|
||
use clone; | ||
use cmp; | ||
use fmt; | ||
use hash; | ||
use intrinsics; | ||
use marker::{Copy, PhantomData, Sized}; | ||
use ptr; | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
|
@@ -647,3 +652,80 @@ pub fn drop<T>(_x: T) { } | |
pub unsafe fn transmute_copy<T, U>(src: &T) -> U { | ||
ptr::read(src as *const T as *const U) | ||
} | ||
|
||
/// Opaque type representing the discriminant of an enum. | ||
/// | ||
/// See the `discriminant` function in this module for more information. | ||
#[unstable(feature = "discriminant_value", reason = "recently added, follows RFC", issue = "24263")] | ||
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. IIRC the "reason" field is no longer required since the contents always ended up saying basically the same thing. |
||
pub struct Discriminant<T>(u64, PhantomData<*const T>); | ||
|
||
// N.B. These trait implementations cannot be derived because we don't want any bounds on T. | ||
|
||
#[unstable(feature = "discriminant_value", reason = "recently added, follows RFC", issue = "24263")] | ||
impl<T> Copy for Discriminant<T> {} | ||
|
||
#[unstable(feature = "discriminant_value", reason = "recently added, follows RFC", issue = "24263")] | ||
impl<T> clone::Clone for Discriminant<T> { | ||
fn clone(&self) -> Self { | ||
*self | ||
} | ||
} | ||
|
||
#[unstable(feature = "discriminant_value", reason = "recently added, follows RFC", issue = "24263")] | ||
impl<T> cmp::PartialEq for Discriminant<T> { | ||
fn eq(&self, rhs: &Self) -> bool { | ||
self.0 == rhs.0 | ||
} | ||
} | ||
|
||
#[unstable(feature = "discriminant_value", reason = "recently added, follows RFC", issue = "24263")] | ||
impl<T> cmp::Eq for Discriminant<T> {} | ||
|
||
#[unstable(feature = "discriminant_value", reason = "recently added, follows RFC", issue = "24263")] | ||
impl<T> hash::Hash for Discriminant<T> { | ||
fn hash<H: hash::Hasher>(&self, state: &mut H) { | ||
self.0.hash(state); | ||
} | ||
} | ||
|
||
#[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 { | ||
fmt.debug_tuple("Discriminant") | ||
.field(&self.0) | ||
.finish() | ||
} | ||
} | ||
|
||
/// Returns a value uniquely identifying the enum variant in `v`. | ||
/// | ||
/// If `T` is not an enum, calling this function will not result in undefined behavior, but the | ||
/// return value is unspecified. | ||
/// | ||
/// # Stability | ||
/// | ||
/// The discriminant of an enum variant may change if the enum definition changes. A discriminant | ||
/// of some variant will not change between compilations with the same compiler. | ||
/// | ||
/// # Examples | ||
/// | ||
/// This can be used to compare enums that carry data, while disregarding | ||
/// the actual data: | ||
/// | ||
/// ``` | ||
/// #![feature(discriminant_value)] | ||
/// use std::mem; | ||
/// | ||
/// enum Foo { A(&'static str), B(i32), C(i32) } | ||
/// | ||
/// assert!(mem::discriminant(&Foo::A("bar")) == mem::discriminant(&Foo::A("baz"))); | ||
/// assert!(mem::discriminant(&Foo::B(1)) == mem::discriminant(&Foo::B(2))); | ||
/// assert!(mem::discriminant(&Foo::B(3)) != mem::discriminant(&Foo::C(3))); | ||
/// ``` | ||
#[unstable(feature = "discriminant_value", reason = "recently added, follows RFC", issue = "24263")] | ||
pub fn discriminant<T>(v: &T) -> Discriminant<T> { | ||
unsafe { | ||
Discriminant(intrinsics::discriminant_value(v), PhantomData) | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(discriminant_value)] | ||
|
||
use std::mem; | ||
|
||
enum ADT { | ||
First(u32, u32), | ||
Second(u64) | ||
} | ||
|
||
pub fn main() { | ||
assert!(mem::discriminant(&ADT::First(0,0)) == mem::discriminant(&ADT::First(1,1))); | ||
assert!(mem::discriminant(&ADT::Second(5)) == mem::discriminant(&ADT::Second(6))); | ||
assert!(mem::discriminant(&ADT::First(2,2)) != mem::discriminant(&ADT::Second(2))); | ||
|
||
let _ = mem::discriminant(&10); | ||
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. this could be `assert_eq!(mem::discriminant(&10), mem::discriminant(&20)); 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. The return value for non-enums is unspecified but that doesn't imply it's the same for all non-enums (it is zero, in truth, but...). |
||
let _ = mem::discriminant(&"test"); | ||
} | ||
|
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 could link to the function in question.
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 guess I can do this in a follow-up PR since this already got r+'ed.