Trait std::marker::Reflect [] [src]

pub trait Reflect { }
Deprecated since 1.14.0

: Specialization makes parametricity impossible

🔬 This is a nightly-only experimental API.   (reflect_marker)

requires RFC and more experience

Types that can be reflected over.

By "reflection" we mean use of the Any trait, or related machinery such as TypeId.

Reflect 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.

#![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 bound T: Reflect, foo would not typecheck. (As a matter of style, it would be preferable to write T: Any, because T: Any implies T: Reflect and T: 'static, but we use Reflect here for illustrative purposes.)

The Reflect bound serves to alert foo's caller to the fact that foo may behave differently depending on whether T is u32 or not. 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". Despite the use of Reflect, Rust lacks true parametricity because a generic function can, at the very least, call mem::size_of without employing any trait bounds whatsoever.

Implementors