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>
impl<T> SyncCompletion<T>
Sourcepub fn new() -> (Self, SyncCompletionPtr)
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:
completionis used to wait for and retrieve the resultcontext_ptrshould be passed to the FFI callback
Sourcepub unsafe fn complete_ok(context: SyncCompletionPtr, value: T)
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.
Sourcepub unsafe fn complete_err(context: SyncCompletionPtr, error: String)
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.
Sourcepub unsafe fn complete_with_result(
context: SyncCompletionPtr,
result: Result<T, String>,
)
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<()>
impl SyncCompletion<()>
Sourcepub unsafe extern "C" fn callback(
context: *mut c_void,
success: bool,
msg: *const c_char,
)
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.