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>
impl<'a> Reference<'a>
Sourcepub fn new<S: StableDeref<'a>>(data: S) -> Self
pub fn new<S: StableDeref<'a>>(data: S) -> Self
Create a reference from any type that implements StableDeref.
This includes &Box
Sourcepub fn from_box<T: ?Sized>(data: &'a Box<T>) -> Self
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.
Sourcepub fn from_arc<T: ?Sized>(data: &'a Arc<T>) -> Self
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.
Sourcepub fn from_pin_box<T: ?Sized>(data: &'a Pin<Box<T>>) -> Self
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.
Sourcepub unsafe fn from_stable<T: ?Sized>(data: &'a T) -> Self
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