pub trait AsSelf<'slf> {
type Ref: ?Sized;
// Required method
fn as_self(&'slf self) -> &Self::Ref;
}
Expand description
Safe downcasting of dependent lifetime bounds on structs.
This trait is similar to AsRef
, except that it allows to capture the lifetime of the own
instance at the time of the borrow. This allows to force it onto the type’s lifetime bounds.
This is particularly useful when the type’s lifetime is somehow tied to it’s own existence, such
as in self-referential structs. See SelfCell
for an implementation that makes use of this.
§Implementation
While this trait may be implemented for any type, it is only useful for types that specify a
lifetime bound, such as Cow
or ByteView
. To implement, define Ref
as the type with all
dependent lifetimes set to 'slf
. Then, simply return self
in as_self
.
use symbolic_common::AsSelf;
struct Foo<'a>(&'a str);
impl<'slf> AsSelf<'slf> for Foo<'_> {
type Ref = Foo<'slf>;
fn as_self(&'slf self) -> &Self::Ref {
self
}
}
§Interior Mutability
Note that if your type uses interior mutability (essentially any type from std::sync
, but
specifically everything built on top of UnsafeCell
), this implicit coercion will not work. The
compiler imposes this limitation by declaring any lifetime on such types as invariant, to avoid
interior mutations to write back data with the lowered lifetime.
If you are sure that your type will not borrow and store data of the lower lifetime, then implement the coercion with an unsafe transmute:
use std::cell::UnsafeCell;
use symbolic_common::AsSelf;
struct Foo<'a>(UnsafeCell<&'a str>);
impl<'slf> AsSelf<'slf> for Foo<'_> {
type Ref = Foo<'slf>;
fn as_self(&'slf self) -> &Self::Ref {
unsafe { std::mem::transmute(self) }
}
}