opaque

Macro opaque 

Source
macro_rules! opaque {
    (
        impl Opaque for $key:ty {
            type Kind<$l:lifetime> = $kind:ty;
        } $(where $($bounds:tt)*)?
    ) => { ... };
    (
        impl[$($params:tt)+] Opaque for $key:ty {
            type Kind<$l:lifetime> = $kind:ty;
        } $(where $($bounds:tt)*)?
    ) => { ... };
}
Expand description

Creates an opaqueified self-referential struct “key”.

Safe wrapper around Opaque that checks the soundness requirements at compile-time.

There are 2 forms of this macro. The second form accepts type parameters.

Note that where bounds go after the impl block.

§Examples

Simple example:

use core::cell::Cell;
 
use selfref::opaque;

struct Foo<'a> {
    foo: Cell<Option<&'a Foo<'a>>>,
}

struct FooKey;
opaque! {
    impl Opaque for FooKey {
        type Kind<'a> = Foo<'a>;
    }
}

Type parameters and where bounds:

use core::cell::Cell;
use core::fmt::Display;
use core::marker::PhantomData;
 
use selfref::opaque;

struct Foo<'a, T: Display> {
    foo: Cell<Option<&'a Foo<'a, T>>>,
    t: T,
}

struct FooKey<T>(PhantomData<T>);
opaque! {
    impl[T] Opaque for FooKey<T> {
        type Kind<'a> = Foo<'a, T>;
    } where T: Display
}