Support arrays in type reflection Tracking issue: rust-lang/rust#146922 This PR adds support for inspecting arrays `[T; N]` through type reflection. It does so by adding `TypeKind::Array` and the `Array` struct: ```rust pub struct Array { pub element_ty: TypeId, pub len: usize, } ``` This can be used to inspect arrays like so: ```rust match const { Type::of::<[u16; 4]>() }.kind { TypeKind::Array(array) => { assert_eq!(array.element_ty, TypeId::of::<u16>()); assert_eq!(array.len, 4); } _ => unreachable!(), } ``` r? @oli-obk
33 lines
810 B
Rust
33 lines
810 B
Rust
#![feature(type_info)]
|
|
//@ run-pass
|
|
//@ check-run-results
|
|
#![allow(dead_code)]
|
|
|
|
use std::mem::type_info::Type;
|
|
|
|
struct Foo {
|
|
a: u32,
|
|
}
|
|
|
|
enum Bar {
|
|
Some(u32),
|
|
None,
|
|
Foomp { a: (), b: &'static str },
|
|
}
|
|
|
|
struct Unsized {
|
|
x: u16,
|
|
s: str,
|
|
}
|
|
|
|
fn main() {
|
|
println!("{:#?}", const { Type::of::<(u8, u8, ())>() }.kind);
|
|
println!("{:#?}", const { Type::of::<[u8; 2]>() }.kind);
|
|
println!("{:#?}", const { Type::of::<Foo>() }.kind);
|
|
println!("{:#?}", const { Type::of::<Bar>() }.kind);
|
|
println!("{:#?}", const { Type::of::<&Unsized>() }.kind);
|
|
println!("{:#?}", const { Type::of::<&str>() }.kind);
|
|
println!("{:#?}", const { Type::of::<&[u8]>() }.kind);
|
|
println!("{:#?}", const { Type::of::<str>() }.kind);
|
|
println!("{:#?}", const { Type::of::<[u8]>() }.kind);
|
|
}
|