Skip to main content

Reference

Struct Reference 

Source
pub struct Reference<'a> { /* private fields */ }
Expand description

See ArgData for details. A reference to data that will be passed through FFI.

§Safety

The referenced data must outlive the NSI context that uses this reference. The data must remain at a stable memory address.

This type now properly enforces pinning by requiring heap-allocated data through Box, Arc, or similar types that guarantee stable addresses.

Implementations§

Source§

impl<'a> Reference<'a>

Source

pub fn new<S: StableDeref<'a>>(data: S) -> Self

Create a reference from any type that implements StableDeref. This includes &Box, &Arc, and &Pin<Box>.

Source

pub fn from_box<T: ?Sized>(data: &'a Box<T>) -> Self

Create a reference from a Box.

Box guarantees a stable heap address, making it safe for FFI.

Source

pub fn from_arc<T: ?Sized>(data: &'a Arc<T>) -> Self

Create a reference from an Arc.

Arc guarantees a stable heap address, making it safe for FFI.

Source

pub fn from_pin_box<T: ?Sized>(data: &'a Pin<Box<T>>) -> Self

Create a reference from a pinned Box.

This is the safest option as it guarantees the data cannot be moved.

Source

pub unsafe fn from_stable<T: ?Sized>(data: &'a T) -> Self

Create a reference from data with a stable address.

§Safety

The caller must guarantee that data has a stable address for its entire lifetime. This is automatically true for:

  • Heap allocated types (Box, Arc, Vec’s data, String’s data)
  • Static data
  • Pinned data

This is NOT safe for:

  • Stack allocated data that might move
  • Data inside collections that might reallocate
Source

pub unsafe fn from_ptr(ptr: *const c_void) -> Self

Create a reference from a raw pointer.

§Safety

The caller must ensure that:

  1. The pointer is valid and non-null
  2. The pointed-to data outlives ’a
  3. The data won’t be moved or freed while this reference exists

Trait Implementations§

Source§

impl<'a> Clone for Reference<'a>

Source§

fn clone(&self) -> Reference<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Reference<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, 'b> From<Reference<'b>> for ArgData<'a, 'b>

Source§

fn from(v: Reference<'b>) -> ArgData<'a, 'b>

Converts to this type from the input type.
Source§

impl<'a, 'b> TryInto<Reference<'b>> for ArgData<'a, 'b>

Source§

type Error = &'static str

The type returned in the event of a conversion error.
Source§

fn try_into( self, ) -> Result<Reference<'b>, <Self as TryInto<Reference<'b>>>::Error>

Performs the conversion.
Source§

impl Send for Reference<'static>

Source§

impl Sync for Reference<'static>

Auto Trait Implementations§

§

impl<'a> Freeze for Reference<'a>

§

impl<'a> RefUnwindSafe for Reference<'a>

§

impl<'a> Unpin for Reference<'a>

§

impl<'a> UnsafeUnpin for Reference<'a>

§

impl<'a> UnwindSafe for Reference<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.