Trait starlark::any::AnyLifetime

source ·
pub unsafe trait AnyLifetime<'a>: 'a {
    // Required methods
    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 starlark::any::ProvidesStaticType;
#[derive(ProvidesStaticType)]
struct Foo1();
#[derive(ProvidesStaticType)]
struct Foo2<'a>(&'a ());

For more complicated context or constraints, you can implement ProvidesStaticType directly.

use starlark::any::ProvidesStaticType;
struct Baz<T: Display>(T);
unsafe impl<'a, T> ProvidesStaticType<'a> for Baz<T>
where
    T: ProvidesStaticType<'a> + Display,
    T::StaticType: Display + Sized,
{
    type StaticType = Baz<T::StaticType>;
}

Required Methods§

source

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.

source

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. Must not consult the self parameter in any way.

Implementations§

source§

impl<'a> dyn AnyLifetime<'a>

source

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

Is the value of type T.

source

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

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

source

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

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

Implementors§

source§

impl<'a, T: ProvidesStaticType<'a> + 'a + ?Sized> AnyLifetime<'a> for T

Any ProvidesStaticType can implement AnyLifetime.

Note ProvidesStaticType and AnyLifetime cannot be the same type, because AnyLifetime need to be object safe, and ProvidesStaticType has type member.