symbolic_common

Trait AsSelf

Source
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) }
    }
}

Required Associated Types§

Source

type Ref: ?Sized

The Self type with 'slf lifetimes, returned by as_self.

Required Methods§

Source

fn as_self(&'slf self) -> &Self::Ref

Returns a reference to self with downcasted lifetime.

Implementations on Foreign Types§

Source§

impl AsSelf<'_> for str

Source§

type Ref = str

Source§

fn as_self(&self) -> &Self::Ref

Source§

impl AsSelf<'_> for u8

Source§

type Ref = u8

Source§

fn as_self(&self) -> &Self::Ref

Source§

impl<'slf, T> AsSelf<'slf> for &'slf T
where T: AsSelf<'slf> + ?Sized,

Source§

type Ref = <T as AsSelf<'slf>>::Ref

Source§

fn as_self(&'slf self) -> &Self::Ref

Source§

impl<'slf, T> AsSelf<'slf> for &'slf mut T
where T: AsSelf<'slf> + ?Sized,

Source§

type Ref = <T as AsSelf<'slf>>::Ref

Source§

fn as_self(&'slf self) -> &Self::Ref

Source§

impl<'slf, T> AsSelf<'slf> for [T]
where T: AsSelf<'slf>, T::Ref: Sized,

Source§

type Ref = [<T as AsSelf<'slf>>::Ref]

Source§

fn as_self(&'slf self) -> &Self::Ref

Source§

impl<'slf, T> AsSelf<'slf> for Rc<T>
where T: AsSelf<'slf>,

Source§

type Ref = <T as AsSelf<'slf>>::Ref

Source§

fn as_self(&'slf self) -> &Self::Ref

Source§

impl<'slf, T> AsSelf<'slf> for Arc<T>
where T: AsSelf<'slf>,

Source§

type Ref = <T as AsSelf<'slf>>::Ref

Source§

fn as_self(&'slf self) -> &Self::Ref

Source§

impl<'slf, T> AsSelf<'slf> for Vec<T>
where T: AsSelf<'slf>, T::Ref: Sized,

Source§

type Ref = [<T as AsSelf<'slf>>::Ref]

Source§

fn as_self(&'slf self) -> &Self::Ref

Implementors§