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

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

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

fn static_type_id() -> TypeId where
    Self: Sized

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

fn static_type_of(&self) -> TypeId

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

Loading content...

Implementations

impl<'a> dyn AnyLifetime<'a>[src]

pub fn is<T: AnyLifetime<'a>>(&self) -> bool[src]

Is the value of type T.

pub fn downcast_ref<T: AnyLifetime<'a>>(&self) -> Option<&T>[src]

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

pub fn downcast_mut<T: AnyLifetime<'a>>(&mut self) -> Option<&mut T>[src]

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

Implementations on Foreign Types

impl<'_> AnyLifetime<'_> for ()[src]

impl<'_> AnyLifetime<'_> for bool[src]

impl<'_> AnyLifetime<'_> for u8[src]

impl<'_> AnyLifetime<'_> for u16[src]

impl<'_> AnyLifetime<'_> for u32[src]

impl<'_> AnyLifetime<'_> for u64[src]

impl<'_> AnyLifetime<'_> for u128[src]

impl<'_> AnyLifetime<'_> for usize[src]

impl<'_> AnyLifetime<'_> for i8[src]

impl<'_> AnyLifetime<'_> for i16[src]

impl<'_> AnyLifetime<'_> for i32[src]

impl<'_> AnyLifetime<'_> for i64[src]

impl<'_> AnyLifetime<'_> for i128[src]

impl<'_> AnyLifetime<'_> for isize[src]

impl<'_> AnyLifetime<'_> for String[src]

impl<'_> AnyLifetime<'_> for Box<str>[src]

Loading content...

Implementors

Loading content...