has_static_form

Macro has_static_form 

Source
macro_rules! has_static_form {
    (impl $($tail:tt)+) => { ... };
}
Expand description

Implement StaticForm for type.

This macro uses a subset of the impl syntax. However, you do not need to name the StaticForm trait in the macro call or provide a body. Additionally, a 'a lifetime (it doesn’t have to be literally 'a) is required as the first generic.

All generics will automatically be bounded by StaticForm<'a>. To opt out of this use T: 'static in the impl generic definitions. Additionally, extra trait bounds can be placed in a where clause at the end.

See the examples below for the different ways to invoke the macro.

§Limitations

This macro can only implement StaticForm for nameable types. For opaque types you must use the give_opaque_static_form macro to wrap a specific value.

§Examples

use dungeon_cell::has_static_form;
use dungeon_cell::lifetime_type_id::LifetimeTypeId;

// no lifetimes and no generics
struct BasicType;
has_static_form!(impl<'a> BasicType);

// one generic
struct OneGeneric<T>(T);
has_static_form!(impl<'a, T> OneGeneric<T>);

// multiple generics
struct MultipleGeneric<A, B, C>(A, B, C);
has_static_form!(impl<'a, A, B, C> MultipleGeneric<A, B, C>);

// a lifetime
struct Lifetime<'a>(&'a str);
has_static_form!(impl<'a> Lifetime<'a>);

// a lifetime and a generic
struct LifetimeGeneric<'a, T>(&'a T);
has_static_form!(impl<'a, T> LifetimeGeneric<'a, T>);

// a lifetime and a static generic
struct StaticGeneric<'a, T>(&'a T);
has_static_form!(impl<'a, T: 'static> StaticGeneric<'a, T>);

struct WithoutStaticForm;
let _ = LifetimeTypeId::of::<StaticGeneric<WithoutStaticForm>>();

// extra trait bounds
struct AlwaysCopy<T: Copy>(T);
has_static_form!(impl<'a, T> AlwaysCopy<T> where T: Copy);