-
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.
Rollup merge of #73418 - doctorn:variants-intrinsic, r=kennytm
Add unstable `core::mem::variant_count` intrinsic Adds a new `const fn` intrinsic which can be used to determine the number of variants in an `enum`. I've shown this to a couple of people and they invariably ask 'why on earth?', but there's actually a very neat use case: At the moment, if you want to create an opaque array type that's indexed by an `enum` with one element for each variant, you either have to hard-code the number of variants, add a `LENGTH` variant or use a `Vec`, none of which are suitable in general (number of variants could change; pattern matching `LENGTH` becomes frustrating; might not have `alloc`). By including this intrinsic, it becomes possible to write the following: ```rust #[derive(Copy, Clone)] enum OpaqueIndex { A = 0, B, C, } struct OpaqueVec<T>(Box<[T; std::mem::num_variants::<OpaqueIndex>()]>); impl<T> std::ops::Index<OpaqueIndex> for OpaqueVec<T> { type Output = T; fn index(&self, idx: OpaqueIndex) -> &Self::Output { &self.0[idx as usize] } } ``` (We even have a use cases for this in `rustc` and I plan to use it to re-implement the lang-items table.)
- Loading branch information
Showing
8 changed files
with
111 additions
and
5 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
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
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 |
---|---|---|
|
@@ -832,6 +832,7 @@ symbols! { | |
v1, | ||
val, | ||
var, | ||
variant_count, | ||
vec, | ||
Vec, | ||
version, | ||
|
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,47 @@ | ||
// run-pass | ||
#![allow(dead_code)] | ||
#![feature(variant_count)] | ||
#![feature(never_type)] | ||
|
||
use std::mem::variant_count; | ||
|
||
enum Void {} | ||
|
||
enum Foo { | ||
A, | ||
B, | ||
C, | ||
} | ||
|
||
enum Bar { | ||
A, | ||
B, | ||
C, | ||
D(usize), | ||
E { field_1: usize, field_2: Foo }, | ||
} | ||
|
||
struct Baz { | ||
a: u32, | ||
b: *const u8, | ||
} | ||
|
||
const TEST_VOID: usize = variant_count::<Void>(); | ||
const TEST_FOO: usize = variant_count::<Foo>(); | ||
const TEST_BAR: usize = variant_count::<Bar>(); | ||
|
||
const NO_ICE_STRUCT: usize = variant_count::<Baz>(); | ||
const NO_ICE_BOOL: usize = variant_count::<bool>(); | ||
const NO_ICE_PRIM: usize = variant_count::<*const u8>(); | ||
|
||
fn main() { | ||
assert_eq!(TEST_VOID, 0); | ||
assert_eq!(TEST_FOO, 3); | ||
assert_eq!(TEST_BAR, 5); | ||
assert_eq!(variant_count::<Void>(), 0); | ||
assert_eq!(variant_count::<Foo>(), 3); | ||
assert_eq!(variant_count::<Bar>(), 5); | ||
assert_eq!(variant_count::<Option<char>>(), 2); | ||
assert_eq!(variant_count::<Option<!>>(), 2); | ||
assert_eq!(variant_count::<Result<!, !>>(), 2); | ||
} |