Trait symbolic::common::AsSelf [−][src]
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
Loading content...Required methods
pub fn as_self(&'slf self) -> &'slf Self::Ref
[src]
Returns a reference to self
with downcasted lifetime.
Implementations on Foreign Types
impl<'slf, T> AsSelf<'slf> for Vec<T, Global> where
T: AsSelf<'slf>,
<T as AsSelf<'slf>>::Ref: Sized,
[src]
T: AsSelf<'slf>,
<T as AsSelf<'slf>>::Ref: Sized,
type Ref = [<T as AsSelf<'slf>>::Ref]
pub fn as_self(&'slf self) -> &'slf <Vec<T, Global> as AsSelf<'slf>>::Ref
[src]
impl<'slf, T> AsSelf<'slf> for &'slf mut T where
T: AsSelf<'slf> + ?Sized,
[src]
T: AsSelf<'slf> + ?Sized,
type Ref = <T as AsSelf<'slf>>::Ref
pub fn as_self(&'slf self) -> &'slf <&'slf mut T as AsSelf<'slf>>::Ref
[src]
impl<'slf, T> AsSelf<'slf> for &'slf T where
T: AsSelf<'slf> + ?Sized,
[src]
T: AsSelf<'slf> + ?Sized,
type Ref = <T as AsSelf<'slf>>::Ref
pub fn as_self(&'slf self) -> &'slf <&'slf T as AsSelf<'slf>>::Ref
[src]
impl<'slf, T> AsSelf<'slf> for Arc<T> where
T: AsSelf<'slf>,
[src]
T: AsSelf<'slf>,
type Ref = <T as AsSelf<'slf>>::Ref
pub fn as_self(&'slf self) -> &'slf <Arc<T> as AsSelf<'slf>>::Ref
[src]
impl<'_> AsSelf<'_> for u8
[src]
impl<'slf, T> AsSelf<'slf> for [T] where
T: AsSelf<'slf>,
<T as AsSelf<'slf>>::Ref: Sized,
[src]
T: AsSelf<'slf>,
<T as AsSelf<'slf>>::Ref: Sized,
type Ref = [<T as AsSelf<'slf>>::Ref]
pub fn as_self(&'slf self) -> &'slf <[T] as AsSelf<'slf>>::Refⓘ
[src]
impl<'slf, T> AsSelf<'slf> for Rc<T> where
T: AsSelf<'slf>,
[src]
T: AsSelf<'slf>,
type Ref = <T as AsSelf<'slf>>::Ref
pub fn as_self(&'slf self) -> &'slf <Rc<T> as AsSelf<'slf>>::Ref
[src]
impl<'_> AsSelf<'_> for str
[src]
Loading content...Implementors
impl<'slf, 'd> AsSelf<'slf> for FatMachO<'d> where
'd: 'slf,
[src]
'd: 'slf,
type Ref = FatMachO<'slf>
pub fn as_self(&'slf self) -> &'slf <FatMachO<'d> as AsSelf<'slf>>::Ref
[src]
impl<'slf, 'd> AsSelf<'slf> for MachArchive<'d> where
'd: 'slf,
[src]
'd: 'slf,
type Ref = MachArchive<'slf>
pub fn as_self(&'slf self) -> &'slf <MachArchive<'d> as AsSelf<'slf>>::Ref
[src]
impl<'slf, 'd> AsSelf<'slf> for MachObject<'d> where
'd: 'slf,
[src]
'd: 'slf,
type Ref = MachObject<'slf>
pub fn as_self(&'slf self) -> &'slf <MachObject<'d> as AsSelf<'slf>>::Ref
[src]
impl<'slf, 'd> AsSelf<'slf> for Archive<'d> where
'd: 'slf,
[src]
'd: 'slf,
impl<'slf, 'd> AsSelf<'slf> for WasmObject<'d> where
'd: 'slf,
[src]
'd: 'slf,
type Ref = WasmObject<'slf>
pub fn as_self(&'slf self) -> &'slf <WasmObject<'d> as AsSelf<'slf>>::Ref
[src]
impl<'slf, 'd> AsSelf<'slf> for SymCache<'d> where
'd: 'slf,
[src]
'd: 'slf,
type Ref = SymCache<'slf>
pub fn as_self(&'slf self) -> &'slf <SymCache<'d> as AsSelf<'slf>>::Ref
[src]
impl<'slf, 'data> AsSelf<'slf> for Object<'data> where
'data: 'slf,
[src]
'data: 'slf,
type Ref = Object<'slf>
pub fn as_self(&'slf self) -> &'slf <Object<'data> as AsSelf<'slf>>::Ref
[src]
impl<'slf, 'data> AsSelf<'slf> for BreakpadObject<'data> where
'data: 'slf,
[src]
'data: 'slf,
type Ref = BreakpadObject<'slf>
pub fn as_self(&'slf self) -> &'slf <BreakpadObject<'data> as AsSelf<'slf>>::Ref
[src]
impl<'slf, 'data> AsSelf<'slf> for ElfObject<'data> where
'data: 'slf,
[src]
'data: 'slf,
type Ref = ElfObject<'slf>
pub fn as_self(&'slf self) -> &'slf <ElfObject<'data> as AsSelf<'slf>>::Ref
[src]
impl<'slf, 'data> AsSelf<'slf> for PdbObject<'data> where
'data: 'slf,
[src]
'data: 'slf,
type Ref = PdbObject<'slf>
pub fn as_self(&'slf self) -> &'slf <PdbObject<'data> as AsSelf<'slf>>::Ref
[src]
impl<'slf, 'data> AsSelf<'slf> for PeObject<'data> where
'data: 'slf,
[src]
'data: 'slf,
type Ref = PeObject<'slf>
pub fn as_self(&'slf self) -> &'slf <PeObject<'data> as AsSelf<'slf>>::Ref
[src]
impl<'slf, 'data> AsSelf<'slf> for SourceBundle<'data> where
'data: 'slf,
[src]
'data: 'slf,