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() -> (SyncCompletion<T>, *mut c_void)
pub fn new() -> (SyncCompletion<T>, *mut c_void)
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 fn wait(self) -> Result<T, String>
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.
pub fn wait_timeout(self, timeout: Duration) -> Option<Result<T, String>>
pub fn context_ptr(&self) -> *mut c_void
Sourcepub unsafe fn complete_ok(context: *mut c_void, value: T)
pub unsafe fn complete_ok(context: *mut c_void, value: T)
Signal successful completion with a value
§Safety
context must be the exact live pointer returned by
SyncCompletion::new or SyncCompletion::context_ptr. This
consumes the callback-owned Arc reference and must be invoked
exactly once for that context. T must be Send if this is called
on a thread other than the one that waits for the result.
Sourcepub unsafe fn complete_err(context: *mut c_void, error: String)
pub unsafe fn complete_err(context: *mut c_void, error: String)
Signal completion with an error
§Safety
context must be the exact live pointer returned by
SyncCompletion::new or SyncCompletion::context_ptr. This
consumes the callback-owned Arc reference and must be invoked
exactly once for that context. T must be Send if this is called
on a thread other than the one that waits for the result.
Sourcepub unsafe fn complete_with_result(
context: *mut c_void,
result: Result<T, String>,
)
pub unsafe fn complete_with_result( context: *mut c_void, result: Result<T, String>, )
Signal completion with a result
§Safety
context must be the exact pointer returned by
SyncCompletion::new or SyncCompletion::context_ptr, its
allocation must remain live for this entire call, and foreign code
must invoke this completion exactly once and never use the pointer
afterward.
T must be Send if this is called on a thread other than the one
that waits for the result: the value moves to the waiting thread, and
it is dropped on the calling thread if the waiter has already gone.
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 i8,
)
pub unsafe extern "C" fn callback( context: *mut c_void, success: bool, msg: *const i8, )
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.