CallbackSharedState

Struct CallbackSharedState 

Source
pub struct CallbackSharedState<Base, Inner> { /* private fields */ }
Expand description

Implements the basic pattern of a two-sided struct used by a system api that calls a callback (which uses the C generic) and a rust abstraction over that API to provide either rust callbacks or futures, where the two sides need to coordinate.

§Safety

This object relies on its layout being such that a pointer to its first member is also a pointer to the struct as a whole, so that references to it can be safely converted back and forth.

Implementations§

Source§

impl<B, I> CallbackSharedState<B, I>

Source

pub fn new(base: B, inner: I) -> Arc<Self>

Creates a new shared state object in an Arc that can be easily manipulated into the pointer types needed for interaction with a C API.

After calling this, the base value will be the one given to the C API and will not be available to rust code without going through the pointer. The idea is that while this object is alive, the C code owns that data. The inner object can be accessed on this through the implementation of std::ops::Deref.

Source

pub fn make_raw_ptr(this: Arc<Self>) -> *mut B

Transforms this reference to the CallbackSharedState into a pointer to the first element of the struct that can be passed to the C API. Every call to Self::make_raw_ptr must be paired with a corresponding call to either Self::from_raw_ptr or Self::release_raw_ptr or the object will leak.

Note that this returns a mutable pointer because that’s usually what the C API wants. The expectation is that the C API can have mutable access to its own state object, but once we’ve done this we will not access it from rust anymore until/unless we’ve reclaimed ownership of the struct somehow.

Source

pub fn as_raw_ptr(this: &Arc<Self>) -> *mut B

Gets a raw pointer to the first element of this struct that can be passed to a C API without affecting the reference count of the underlying Arc.

Note that this returns a mutable pointer because that’s usually what the C API wants. The expectation is that the C API can have mutable access to its own state object, but once we’ve done this we will not access it from rust anymore until/unless we’ve reclaimed ownership of the struct somehow.

Source

pub unsafe fn from_raw_ptr(this: *mut B) -> Arc<Self>

Converts the given pointer to the base type back to a fully owned Arc to the CallbackSharedState.

This should be used when reclaiming the state object after the callback has been called or synchronously cancelled.

§Safety

This must only ever be called up to once for every Self::make_raw_ptr that has been called. See the safety comments for Arc::from_raw for more information.

Source

pub unsafe fn release_raw_ptr(this: *mut B)

Releases the given pointer to the base type by decrementing the reference count on the original Arc.

This should be used when the callback has been called or synchronously cancelled, but there’s no need to access the base data.

§Safety

This must only ever be called up to once for every Self::make_raw_ptr that has been called. See the safety comments for Arc::decrement_strong_count for more information.

Trait Implementations§

Source§

impl<B, I> Deref for CallbackSharedState<B, I>

Source§

type Target = I

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<Base, Inner> Freeze for CallbackSharedState<Base, Inner>
where Base: Freeze, Inner: Freeze,

§

impl<Base, Inner> RefUnwindSafe for CallbackSharedState<Base, Inner>
where Base: RefUnwindSafe, Inner: RefUnwindSafe,

§

impl<Base, Inner> Send for CallbackSharedState<Base, Inner>
where Base: Send, Inner: Send,

§

impl<Base, Inner> Sync for CallbackSharedState<Base, Inner>
where Base: Sync, Inner: Sync,

§

impl<Base, Inner> Unpin for CallbackSharedState<Base, Inner>
where Base: Unpin, Inner: Unpin,

§

impl<Base, Inner> UnwindSafe for CallbackSharedState<Base, Inner>
where Base: UnwindSafe, Inner: 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.