Crate metatype

Source
Expand description

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.

Structs§

Concrete
Meta data for a concrete, sized type
Slice
Meta data for a slice
TraitObject
Meta data for a trait object

Enums§

MetaType
Meta type of a type

Traits§

Type
Implemented on all types, it provides helper methods to determine whether a type is TraitObject, Slice or Concrete, and work with them respectively.

Functions§

try_type_coerce
Convert from one type parameter to another, where they are the same type. Returns None if the types differ.
type_coerce
Convert from one type parameter to another, where they are the same type. Panics with an explanatory message if the types differ.
type_id
Gets an identifier which is globally unique to the specified type. This function will return the same value for a type regardless of whichever crate it is invoked in.