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(core)] extern crate core; 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(core)] 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
`T:Reflect,
`, foo
`foowould not type check (note: as a matter of style, it would be preferable to to write
` would not type check
(note: as a matter of style, it would be preferable to to write
T:Any
`T:Any, because
`, because T:Any
`T:Anyimplies
` implies T:Reflect
`T:Reflectand
` and T:'static
`T:'static, but we use
`, but
we use Reflect
`Reflecthere to show how it works). The
` here to show how it works). The Reflect
`Reflectbound thus serves to alert
` bound
thus serves to alert foo
`foo's caller to the fact that
`'s caller to the fact that foo
`foomay behave differently depending on whether
` may
behave differently depending on whether T=u32
`T=u32or not. In particular, thanks to the
` or not. In
particular, thanks to the Reflect
`Reflectbound, callers know that a function declared like
` bound, callers know that a
function declared like fn bar<T>(...)
`fn barwill always act in precisely the same way no matter what type
` will always act in
precisely the same way no matter what type T
`Tis supplied, because there are no bounds declared on
` is supplied,
because there are no bounds declared on T
`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".)