GitHub user asukaminato0721 added a comment to the discussion: How to design 
opendal c binding's async api

I've implemented a basic design https://github.com/apache/opendal/pull/6072 for 
comments.

---

## Async APIs

OpenDAL’s C binding mirrors the Rust async operator, but keeps all runtime 
management on the Rust side so C callers never need to embed Tokio. The design 
is intentionally future/await centric:

- `opendal_async_operator_new` builds an async operator that internally holds a 
clone of the core `Operator` plus a handle to OpenDAL’s shared Tokio runtime.
- Each async method (`*_stat`, `*_write`, `*_read`, `*_delete`) immediately 
returns an opaque `opendal_future_*` handle. Creating the future is 
non-blocking—the runtime schedules the real work on its thread pool.
- You stay in control of when to pull the result. Call `opendal_future_*_await` 
to block the current thread until the operation finishes, or 
`opendal_future_*_poll` to integrate with your own event loop without blocking.
- If you abandon an operation, call `opendal_future_*_free` to cancel it. This 
aborts the underlying task and drops any pending output safely.

Because futures carry ownership of the eventual metadata/error objects, the 
`*_await` helpers always transfer heap allocations using the same conventions 
as the blocking API (free metadata with `opendal_metadata_free`, free errors 
with `opendal_error_free`, etc.).


Need non-blocking integration with your own loop? Call 
`opendal_future_stat_poll(fut.future, &out)` inside your loop. It returns 
`OPENDAL_FUTURE_PENDING` until the result is ready; once it reports 
`OPENDAL_FUTURE_READY`, call `opendal_future_stat_await` exactly once to 
consume the output.

See `examples/async_stat.c` for a narrated walkthrough and 
`tests/async_stat_test.cpp` for GoogleTest-based assertions that cover both 
success and error paths.


GitHub link: 
https://github.com/apache/opendal/discussions/6082#discussioncomment-15045568

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]

Reply via email to