Struct Userdata

Source
pub struct Userdata<T: 'static>(/* private fields */);
Expand description

Type-erased user data.

Use this to wrap arbitrary data into a structure which may be passed via a c_void pointer to an external library. Later, when this extern callback is run with that data, you may unwrap it and can thus access the original data.

The implementation uses associated methods:

Each prepare() must be paired with exactly one consume() to release the data held by the wrapper structure. peek_at() must only be called in-between and the lifetime of the returned reference is not allowed to extend past the next call to either peek_at() or consume().

§Examples

use open62541::Userdata;

// Turn user data into type-erased void pointer for FFI.
let raw_data: *mut c_void = Userdata::<u32>::prepare(0);

// Use type-erased pointer to get/manipulate user data.
unsafe {
    let userdata = Userdata::<u32>::peek_at(raw_data);
    assert_eq!(*userdata, 0);
    *userdata = 123;
}

// Unwrap data. Clean up resources held by pointer.
let userdata = unsafe {
    Userdata::<u32>::consume(raw_data)
};

// Got user data. `raw_data` is no longer valid.
assert_eq!(userdata, 123);

Implementations§

Source§

impl<T> Userdata<T>

Source

pub fn prepare(userdata: T) -> *mut c_void

Wraps user data.

This allocates memory. To prevent memory leaks, make sure to call consume() on the returned pointer exactly once.

Source

pub fn prepare_sentinel(userdata: T) -> UserdataSentinel<T>

Wraps user data and returns sentinel.

This uses prepare() but wraps the resulting pointer in UserdataSentinel to make sure that it is cleaned up when the sentinel is dropped. This is useful when the recipient of the raw pointer does not become the owner, i.e. does not clean up the user data by itself.

Use UserdataSentinel::as_ptr() to get the pointer from the sentinel.

Source

pub unsafe fn peek_at<'a>(data: *mut c_void) -> &'a mut T

Unwraps c_void pointer to access data.

§Safety

The given pointer must have been returned from prepare(), using the same value type T. It must not have been given to consume() yet.

The lifetime of the returned reference is not allowed to extend past the next call to either peek_at() or consume() and must not outlive the lifetime of T itself. (In case the user data has been wrapped into a UserdataSentinel, the sentinel must still be alive.)

Source

pub unsafe fn consume(data: *mut c_void) -> T

Unwraps c_void pointer and returns owned data.

§Safety

The given pointer must have been returned from prepare(), using the same value type T. It must not have been given to consume() yet, nor wrapped in a UserdataSentinel (the user data is consumed automatically when the sentinel is being dropped).

Trait Implementations§

Source§

impl<T: Debug + 'static> Debug for Userdata<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Userdata<T>

§

impl<T> RefUnwindSafe for Userdata<T>
where T: RefUnwindSafe,

§

impl<T> Send for Userdata<T>
where T: Send,

§

impl<T> Sync for Userdata<T>
where T: Sync,

§

impl<T> Unpin for Userdata<T>
where T: Unpin,

§

impl<T> UnwindSafe for Userdata<T>
where T: UnwindSafe,

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> 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, 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.