[][src]Trait symbolic_common::AsSelf

pub trait AsSelf<'slf> {
    type Ref: ?Sized;
    fn as_self(&'slf self) -> &Self::Ref;
}

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.

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:

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

Associated Types

type Ref: ?Sized

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

Loading content...

Required methods

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

Returns a reference to self with downcasted lifetime.

Loading content...

Implementations on Foreign Types

impl<'_> AsSelf<'_> for u8[src]

type Ref = u8

impl<'_> AsSelf<'_> for str[src]

type Ref = str

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

type Ref = [T::Ref]

impl<'slf, T> AsSelf<'slf> for &'slf T where
    T: AsSelf<'slf>, 
[src]

type Ref = T::Ref

impl<'slf, T> AsSelf<'slf> for &'slf mut T where
    T: AsSelf<'slf>, 
[src]

type Ref = T::Ref

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

type Ref = [T::Ref]

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

type Ref = T::Ref

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

type Ref = T::Ref

Loading content...

Implementors

Loading content...