Expand description
An opaqueified self-referential struct “key”.
Safety
This is unsafe because there are a bunch of soundness invariants that need to be upheld. The following list is non-exhaustive:
Kind
must not have aDrop
impl in any “path” that may trace back to the original self-referential type, if saidDrop
impl can observe the self-referential type.- We assume
Kind
has the same layout for any'a
. This is true as of the time of writing this, and relies on Rust not having lifetime specialization.
Examples
#![feature(generic_associated_types)]
use core::cell::Cell;
use selfref::SelfRef;
use selfref::Opaque;
struct Foo<'a> {
foo: Cell<Option<&'a Foo<'a>>>,
}
impl<'a> SelfRef<'a> for Foo<'a> {}
struct FooKey;
// SAFETY: Foo has no Drop impl and has the same layout for any 'a.
unsafe impl Opaque for FooKey {
type Kind<'a> = Foo<'a>;
}