Crate type_info [−] [src]
An implementation of detailed type information and reflection.
This library provides simple access to type information at runtime, as well as the ability to manipulate data whose type is not statically known.
Type information
By deriving the TypeInfo
trait, you get access to type information about the type at runtime.
#![feature(const_type_id)] extern crate type_info; #[macro_use] extern crate type_info_derive; use type_info::TypeInfo; #[derive(TypeInfo)] struct Person { name: String, age: u32, } fn main() { let ty = Person::TYPE; assert_eq!("Person", ty.ident); assert_eq!(vec!["name", "age"], ty.fields().iter().map(|f| f.ident.unwrap()).collect::<Vec<_>>()); }
Static dispatch
This example shows how to use the main TypeInfo
trait with generic parameters (i.e. not trait
objects):
// Person is defined like in the above example... // A function that can take any type that has a field called "name" of type String. fn add_smith_to_name<A>(anything: &mut A) where A: type_info::TypeInfo { let name = anything.field_mut::<String>(type_info::FieldId::Named("name")).unwrap(); name.push_str(" Smith"); } fn main() { let mut person = Person { name: "Lisa".to_owned(), age: 23, }; add_smith_to_name(&mut person); assert_eq!("Lisa Smith", person.name.as_str()); }
Dynamic dispatch
This example shows how to use the DynamicTypeInfo
trait when you don't want to introduce a
type parameter to specialize a function, but instead prefer to use a trait object:
// Person is defined like in the above example... // A function that can take any type that has a field called "name" of type String. fn add_smith_to_name(anything: &mut type_info::DynamicTypeInfo) { let field = anything.field_any_mut(type_info::FieldId::Named("name")).unwrap(); let name = field.downcast_mut::<String>().unwrap(); name.push_str(" Smith"); } fn main() { let mut person = Person { name: "Lisa".to_owned(), age: 23, }; add_smith_to_name(&mut person); assert_eq!("Lisa Smith", person.name.as_str()); }
Structs
DataEnum |
Data associated with |
DataStruct |
Data associated with |
DataUnion |
Data associated with |
Field |
A field that is associated with a type or |
FieldsNamed |
A set of named fields associated with a type or |
FieldsUnnamed |
A set of unnamed fields associated with a type or |
Type |
Type information for a type that implements |
Variant |
A specific |
Enums
Data |
Data associated with type information. |
FieldId |
A locally unique identifier for a field within a certain type. |
Fields |
A set of fields associated with a type or |
Traits
DynamicTypeInfo |
A type that has compile-time dynamic type information associated with it. |
TryTypeInfo |
A trait that is implemented for every type to conditionally determine whether it exposes type information. |
TypeInfo |
A type that has compile-time static type information associated with it. |
Type Definitions
TypeId |
A globally unique identifier for a type. |