Trait gazebo::any::AnyLifetime[][src]

pub unsafe trait AnyLifetime<'a>: 'a {
    fn static_type_id() -> TypeId
    where
        Self: Sized
;
fn static_type_of(&self) -> TypeId; }
Expand description

Like Any, but while Any requires 'static, this version allows a lifetime parameter.

Code using this trait is unsafe if your implementation of the inner methods do not meet the invariants listed. Therefore, it is recommended you use one of the helper macros.

If your data type is of the form Foo or Foo<'v> you can derive AnyLifetime:

use gazebo::any::AnyLifetime;
#[derive(AnyLifetime)]
struct Foo1();
#[derive(AnyLifetime)]
struct Foo2<'a>(&'a ());

If your type has type arguments, you will often need to derive a separate AnyLifetime instance at every instantiated type. The any_lifetime! macro can help with that. As a special case it can also generate an instance if you have a type with a single lifetime argument.

#[macro_use] extern crate gazebo;
use gazebo::any::AnyLifetime;
struct Bar1<T>(T);
type Bar2 = Bar1<String>;
type Bar3<'v> = Bar1<&'v ()>;
any_lifetime!(Bar1<bool>);
any_lifetime!(Bar2);
any_lifetime!(Bar3<'v>);

For more complicated context or constraints, you can use any_lifetime_body! to implement just the body. It is important that the type passed to any_lifetime_body! is the same as Self but at 'static.

#[macro_use] extern crate gazebo;
use gazebo::any::AnyLifetime;
struct Baz<T>(T);
unsafe impl<'v> AnyLifetime<'v> for Baz<Baz<&'v ()>> {
    any_lifetime_body!(Baz<Baz<&'static ()>>);
}

If we had called any_lifetime_body!(bool) in the example above, that would have violated the invariants of AnyLifetime, leading to unsafe behaviour.

Required methods

Must return the TypeId of Self but where the lifetimes are changed to 'static. Must be consistent with static_type_of.

Must return the TypeId of Self but where the lifetimes are changed to 'static. Must be consistent with static_type_id. Must not consult the self parameter in any way.

Implementations

Is the value of type T.

Downcast a reference to type T, or return None if it is not the right type.

Downcast a mutable reference to type T, or return None if it is not the right type.

Implementations on Foreign Types

Implementors