Trait core::marker::Reflect
[−]
[src]
pub trait Reflect { }
: requires RFC and more experience
A marker trait indicates a type that can be reflected over. This trait is implemented for all types. Its purpose is to ensure that when you write a generic function that will employ reflection, that must be reflected (no pun intended) in the generic bounds of that function. Here is an example:
#![feature(reflect_marker)] fn main() { use std::marker::Reflect; use std::any::Any; fn foo<T:Reflect+'static>(x: &T) { let any: &Any = x; if any.is::<u32>() { println!("u32"); } } }#![feature(reflect_marker)] use std::marker::Reflect; use std::any::Any; fn foo<T:Reflect+'static>(x: &T) { let any: &Any = x; if any.is::<u32>() { println!("u32"); } }
Without the declaration T:Reflect
, foo
`foowould not type check (note: as a matter of style, it would be preferable to to write
T:Any, because
T:Anyimplies
` implies T:Reflect
and T:'static
, but
we use Reflect
`Reflecthere to show how it works). The
Reflectbound thus serves to alert
foo's caller to the fact that
foomay behave differently depending on whether
T=u32or not. In particular, thanks to the
Reflectbound, callers know that a function declared like
fn barwill always act in precisely the same way no matter what type
Tis supplied, because there are no bounds declared on
T`. (The ability for a
caller to reason about what a function may do based solely on what
generic bounds are declared is often called the "parametricity
property".)