#trait-object #slice #vtable #type #meta

nightly metatype

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

6 releases

0.2.2 Mar 19, 2025
0.2.1 Jul 10, 2021
0.2.0 Oct 18, 2019
0.1.2 Jul 23, 2019
0.1.1 Jul 20, 2018

#483 in Rust patterns

Download history 101/week @ 2024-12-04 141/week @ 2024-12-11 77/week @ 2024-12-18 34/week @ 2024-12-25 71/week @ 2025-01-01 82/week @ 2025-01-08 122/week @ 2025-01-15 97/week @ 2025-01-22 72/week @ 2025-01-29 137/week @ 2025-02-05 125/week @ 2025-02-12 112/week @ 2025-02-19 93/week @ 2025-02-26 101/week @ 2025-03-05 129/week @ 2025-03-12 245/week @ 2025-03-19

579 downloads per month
Used in 11 crates (2 directly)

MIT/Apache

15KB
258 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 ptr_metadata, specialization and arbitrary_self_types_pointers 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