-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #36823 - durka:discriminant_value, r=nagisa
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
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
let _ = mem::discriminant(&"test"); | ||
} | ||
|