Skip to main content

SyncCompletion

Struct SyncCompletion 

Source
pub struct SyncCompletion<T> { /* private fields */ }
Expand description

A synchronous completion handler for async FFI callbacks

This type provides a way to block until an async callback completes and retrieve the result. It uses Arc<...> internally for thread-safe signaling between the callback and the waiting thread. The raw context returned by Self::new is exact-live and one-shot; its atomic consumed flag can only diagnose a duplicate callback while the allocation remains live.

Implementations§

Source§

impl<T> SyncCompletion<T>

Source

pub fn new() -> (Self, SyncCompletionPtr)

Create a new completion handler and return the context pointer for FFI

Returns a tuple of (completion, context_ptr) where:

  • completion is used to wait for and retrieve the result
  • context_ptr should be passed to the FFI callback
Source

pub fn wait(self) -> Result<T, String>

Wait for the completion callback and return the result

This method blocks until the callback signals completion.

§Errors

Returns an error string if the callback signaled an error.

§Panics

Panics if the internal mutex is poisoned.

Source

pub unsafe fn complete_ok(context: SyncCompletionPtr, value: T)

Signal successful completion with a value

§Safety

context must be the exact live pointer returned by SyncCompletion::new. This consumes the callback-owned Arc reference and must be invoked exactly once for that context.

Source

pub unsafe fn complete_err(context: SyncCompletionPtr, error: String)

Signal completion with an error

§Safety

context must be the exact live pointer returned by SyncCompletion::new. This consumes the callback-owned Arc reference and must be invoked exactly once for that context.

Source

pub unsafe fn complete_with_result( context: SyncCompletionPtr, result: Result<T, String>, )

Signal completion with a result

§Safety

context must be the exact pointer returned by SyncCompletion::new, its allocation must remain live for this entire call, and foreign code must invoke this completion exactly once and never use the pointer afterward.

The consumed flag is only defence in depth for a duplicate call while the allocation is still live. Checking that flag itself dereferences context; it does not make an already-freed, reused, or concurrently invalidated pointer safe.

Source§

impl SyncCompletion<()>

Source

pub unsafe extern "C" fn callback( context: *mut c_void, success: bool, msg: *const c_char, )

C callback for operations that return (context, success, error_msg)

This can be used directly wherever a crate::ffi_callbacks::UnitCompletionCallback is required.

The body is wrapped in catch_user_panic so that a mutex-poison panic (or any other unexpected panic) does not unwind across the extern "C" boundary, which would be undefined behaviour.

§Safety

context must be the exact live pointer returned with this UnitCompletion, must be invoked exactly once, and must not be used after this call. The internal atomic flag does not protect storage that has already been freed or concurrently invalidated.

When success is false, msg must be null or point to a valid NUL-terminated C string for the duration of this call. It is ignored when success is true.

Trait Implementations§

Source§

impl<T> Default for SyncCompletion<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for SyncCompletion<T>
where Arc<SyncCompletionInner<T>>: Freeze,

§

impl<T> RefUnwindSafe for SyncCompletion<T>
where Arc<SyncCompletionInner<T>>: RefUnwindSafe,

§

impl<T> Send for SyncCompletion<T>
where Arc<SyncCompletionInner<T>>: Send,

§

impl<T> Sync for SyncCompletion<T>
where Arc<SyncCompletionInner<T>>: Sync,

§

impl<T> Unpin for SyncCompletion<T>
where Arc<SyncCompletionInner<T>>: Unpin,

§

impl<T> UnsafeUnpin for SyncCompletion<T>
where Arc<SyncCompletionInner<T>>: UnsafeUnpin,

§

impl<T> UnwindSafe for SyncCompletion<T>
where Arc<SyncCompletionInner<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 = !

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

fn try_from(value: U) -> Result<T, !>

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.