[][src]Struct abi_stable::sabi_types::RRef

#[repr(transparent)]pub struct RRef<'a, T> { /* fields omitted */ }

Equivalent to &'a T, defined as a workaround to allow casting from &T to &U inside a const fn in stable Rust.

Implementations

impl<'a, T> RRef<'a, T>[src]

pub const unsafe fn from_raw(ref_: *const T) -> Self where
    T: 'a, 
[src]

Constructs this RRef from a raw pointer.

Safety

You must ensure that the raw pointer is valid for the 'a lifetime.

Example

use abi_stable::sabi_types::RRef;

struct GetPtr<'a,T>(&'a T);

impl<'a,T:'a> GetPtr<'a,T>{
    const PTR:*const Option<T>=&None;

    const STATIC:RRef<'a,Option<T>>=unsafe{
        RRef::from_raw(Self::PTR)
    };
}

pub const fn new(ref_: &'a T) -> Self[src]

Constructs this RRef from a reference.

Example

use abi_stable::sabi_types::RRef;

struct GetPtr<'a,T>(&'a T);

impl<'a,T:'a> GetPtr<'a,T>{
    const REF:&'a Option<T>=&None;

    const STATIC:RRef<'a,Option<T>>=
        RRef::new(Self::REF);
}

pub fn get(self) -> &'a T[src]

Gets access to the reference.

Use this to get a &'a T, instead of a reference borrowing from the pointer.

Example

use abi_stable::sabi_types::RRef;

struct GetPtr<'a,T>(&'a T);

impl<'a,T:'a> GetPtr<'a,T>{
    const NONE_REF:&'a Option<T>=&None;

    const REFERENCE:RRef<'a,Option<T>>=RRef::new(Self::NONE_REF);

    // This returns a reference that lives as long as T does
    fn returns_ref()->&'a Option<String>{
        let reference=GetPtr::<String>::REFERENCE;
        reference.get()
    }

    // This doesn't work,it borrows the reference `variable`.
    // fn returns_ref_2()->&'a Option<String>{
    //     let reference=GetPtr::<String>::REFERENCE;
    //     &*reference
    // }
}

pub const fn get_raw(self) -> *const T[src]

Gets access to the referenced value,as a raw pointer.

Example

use abi_stable::sabi_types::RRef;
use std::convert::Infallible;

struct GetPtr<'a,T>(&'a T);

impl<'a,T:'a> GetPtr<'a,T>{
    const NONE_REF:&'a Option<T>=&None;

    const STATIC:RRef<'a,Option<T>>=
        RRef::new(Self::NONE_REF);
}

let reference:*const Option<Infallible>=
    GetPtr::<Infallible>::STATIC.get_raw();

pub const fn cast_into_raw<U>(self) -> *const U[src]

Accesses the referenced value as a casted raw pointer.

pub const unsafe fn transmute<'b, U>(self) -> RRef<'b, U> where
    U: 'b, 
[src]

Transmutes this RRef<'a,T> to a RRef<'b,U>.

Safety

This has the same safety problems that transmuting &'a T to &'b U has.

Example

use abi_stable::sabi_types::RRef;

struct GetPtr<'a,T>(&'a T);

impl<'a,T:'a> GetPtr<'a,T>{
    const PTR:*const Option<T>=&None;

    const STATIC:RRef<'a,Option<T>>=unsafe{
        RRef::from_raw(Self::PTR)
    };
}

let reference:RRef<'static,Option<[();0xFFF_FFFF]>>=unsafe{
    GetPtr::<()>::STATIC
        .transmute::<'static,Option<[();0xFFF_FFFF]>>()
};

pub const unsafe fn transmute_ref<U>(self) -> RRef<'a, U> where
    U: 'a, 
[src]

Transmutes this RRef<'a,T> to a RRef<'a,U>.

This is equivalent to calling transmute, except that it doesn't change the lifetime parameter of RRef.

Safety

This has the same safety problems that transmuting &'a T to &'a U has.

Example

use abi_stable::sabi_types::RRef;

struct GetPtr<'a,T>(&'a T);

impl<'a,T:'a> GetPtr<'a,T>{
    const PTR:*const Option<T>=&None;

    const STATIC:RRef<'a,Option<T>>=unsafe{
        RRef::from_raw(Self::PTR)
    };
}

let reference:RRef<'static,[();0xFFF_FFFF]>=unsafe{
    GetPtr::<'static,()>::STATIC
        .transmute_ref::<[();0xFFF_FFFF]>()
};

Trait Implementations

impl<'a, T, U> CanTransmuteElement<U> for RRef<'a, T> where
    U: 'a, 
[src]

type TransmutedPtr = RRef<'a, U>

The type of the pointer after it's element type has been changed.

impl<'a, T> Clone for RRef<'a, T>[src]

impl<'a, T> Copy for RRef<'a, T>[src]

impl<'a, T> Debug for RRef<'a, T> where
    T: Debug
[src]

impl<'a, T> Deref for RRef<'a, T>[src]

type Target = T

The resulting type after dereferencing.

impl<'a, T> Display for RRef<'a, T> where
    T: Display
[src]

impl<'a, T> Eq for RRef<'a, T> where
    T: Eq
[src]

impl<'a, T> GetPointerKind for RRef<'a, T>[src]

type Kind = PK_Reference

The kind of the pointer.

impl<'a, T> GetStaticEquivalent_ for RRef<'a, T> where
    T: __StableAbi,
    T: 'a, 
[src]

type StaticEquivalent = _static_RRef<'static, __GetStaticEquivalent<T>>

impl<'a, T> Hash for RRef<'a, T> where
    T: Hash
[src]

impl<'a, T> Ord for RRef<'a, T> where
    T: Ord
[src]

impl<'a, T> PartialEq<RRef<'a, T>> for RRef<'a, T> where
    T: PartialEq
[src]

impl<'a, T> PartialOrd<RRef<'a, T>> for RRef<'a, T> where
    T: PartialOrd
[src]

impl<'a, T> Send for RRef<'a, T> where
    &'a T: Send
[src]

impl<'a, T> StableAbi for RRef<'a, T> where
    T: __StableAbi,
    T: 'a, 
[src]

type IsNonZeroType = <NonNull<T> as __StableAbi>::IsNonZeroType

Whether this type has a single invalid bit-pattern. Read more

impl<'a, T> Sync for RRef<'a, T> where
    &'a T: Sync
[src]

Auto Trait Implementations

impl<'a, T> RefUnwindSafe for RRef<'a, T> where
    T: RefUnwindSafe

impl<'a, T> Unpin for RRef<'a, T>

impl<'a, T> UnwindSafe for RRef<'a, T> where
    T: RefUnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<'a, T> BorrowOwned<'a> for T where
    T: 'a + Clone
[src]

type ROwned = T

The owned type, stored in RCow::Owned

type RBorrowed = &'a T

The borrowed type, stored in RCow::Borrowed

impl<T> From<T> for T[src]

impl<T> GetWithMetadata for T[src]

type ForSelf = WithMetadata_<T, T>

This is always WithMetadata_<Self, Self>

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> SelfOps for T where
    T: ?Sized
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<This> TransmuteElement for This where
    This: ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The error type returned when the conversion fails.

impl<T> TypeIdentity for T where
    T: ?Sized
[src]

type Type = T

The same type as Self. Read more

impl<This> ValidTag_Bounds for This where
    This: Debug + Clone + PartialEq<This>, 
[src]