#meta #types #trait-object #vtable #slice

nightly metatype

Helper methods to determine whether a type is TraitObject, Slice or Concrete, and work with them respectively

5 releases

0.2.1 Jul 10, 2021
0.2.0 Oct 18, 2019
0.1.2 Jul 23, 2019
0.1.1 Jul 20, 2018
0.1.0 Jul 20, 2018

#903 in Rust patterns

Download history 86/week @ 2022-06-14 128/week @ 2022-06-21 99/week @ 2022-06-28 94/week @ 2022-07-05 79/week @ 2022-07-12 84/week @ 2022-07-19 95/week @ 2022-07-26 134/week @ 2022-08-02 163/week @ 2022-08-09 89/week @ 2022-08-16 100/week @ 2022-08-23 122/week @ 2022-08-30 120/week @ 2022-09-06 104/week @ 2022-09-13 107/week @ 2022-09-20 103/week @ 2022-09-27

448 downloads per month
Used in 9 crates (2 directly)

MIT/Apache

14KB
255 lines

metatype

Crates.io MIT / Apache 2.0 licensed Build Status

📖 Docs | 💬 Chat

Helper methods to determine whether a type is TraitObject, Slice or Concrete, and work with them respectively.

Examples

assert_eq!(usize::METATYPE, MetaType::Concrete);
assert_eq!(any::Any::METATYPE, MetaType::TraitObject);
assert_eq!(<[u8]>::METATYPE, MetaType::Slice);

let a: Box<usize> = Box::new(123);
assert_eq!(Type::meta_type(&*a), MetaType::Concrete);
let a: Box<dyn any::Any> = a;
assert_eq!(Type::meta_type(&*a), MetaType::TraitObject);

let a = [123,456];
assert_eq!(Type::meta_type(&a), MetaType::Concrete);
let a: &[i32] = &a;
assert_eq!(Type::meta_type(a), MetaType::Slice);

let a: Box<dyn any::Any> = Box::new(123);
let meta: TraitObject = type_coerce(Type::meta(&*a));
println!("vtable: {:?}", meta.vtable);

Note

This currently requires Rust nightly for the raw, specialization and arbitrary_self_types features.

License

Licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps