[][src]Trait symbolic::common::AsSelf

pub trait AsSelf<'slf> {
    type Ref: ?Sized;
    fn as_self(&'slf self) -> &'slf 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.

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

Associated Types

type Ref: ?Sized

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

Loading content...

Required methods

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

Returns a reference to self with downcasted lifetime.

Loading content...

Implementations on Foreign Types

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

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

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

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

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

type Ref = str

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

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

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

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

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

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

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

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

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

type Ref = u8

Loading content...

Implementors

impl<'slf, 'd> AsSelf<'slf> for Object<'d> where
    'd: 'slf, 
[src]

type Ref = Object<'slf>

impl<'slf, 'd> AsSelf<'slf> for BreakpadObject<'d> where
    'd: 'slf, 
[src]

type Ref = BreakpadObject<'slf>

impl<'slf, 'd> AsSelf<'slf> for ElfObject<'d> where
    'd: 'slf, 
[src]

type Ref = ElfObject<'slf>

impl<'slf, 'd> AsSelf<'slf> for FatMachO<'d> where
    'd: 'slf, 
[src]

type Ref = FatMachO<'slf>

impl<'slf, 'd> AsSelf<'slf> for MachArchive<'d> where
    'd: 'slf, 
[src]

type Ref = MachArchive<'slf>

impl<'slf, 'd> AsSelf<'slf> for MachObject<'d> where
    'd: 'slf, 
[src]

type Ref = MachObject<'slf>

impl<'slf, 'd> AsSelf<'slf> for PdbObject<'d> where
    'd: 'slf, 
[src]

type Ref = PdbObject<'slf>

impl<'slf, 'd> AsSelf<'slf> for PeObject<'d> where
    'd: 'slf, 
[src]

type Ref = PeObject<'slf>

impl<'slf, 'd> AsSelf<'slf> for SourceBundle<'d> where
    'd: 'slf, 
[src]

type Ref = SourceBundle<'slf>

impl<'slf, 'd> AsSelf<'slf> for Archive<'d> where
    'd: 'slf, 
[src]

type Ref = Archive<'slf>

impl<'slf, 'd> AsSelf<'slf> for SymCache<'d> where
    'd: 'slf, 

type Ref = SymCache<'slf>

Loading content...