Skip to content

Commit cf74db9

Browse files
committed
split wit-dylib async feature into -raw and -runtime variations
- `async-runtime`: integrates with `std::future` by way of `wit_bindgen::rt` - `async-raw`: minimal layer over raw bindings; does not use `std::future` or `wit_bindgen` Note that these are mutually exclusive; enabling both will result in a compiler error. Signed-off-by: Joel Dice <[email protected]>
1 parent 416c5da commit cf74db9

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

crates/wit-dylib/ffi/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ doctest = false
1515
wit-bindgen = { workspace = true, optional = true, features = ['async'] }
1616

1717
[features]
18-
default = ['async']
19-
async = ['dep:wit-bindgen']
18+
default = []
19+
async-raw = []
20+
async-runtime = ['dep:wit-bindgen']

crates/wit-dylib/ffi/src/lib.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#![allow(unsafe_code)]
1212
#![allow(clippy::allow_attributes_without_reason)]
1313

14+
#[cfg(all(feature = "async-runtime", feature = "async-raw"))]
15+
compile_error!("cannot enable both `async-runtime` and `async-raw` features");
16+
1417
use std::alloc::Layout;
1518
use std::ptr;
1619

@@ -372,17 +375,17 @@ pub trait Interpreter: 'static {
372375

373376
fn export_call(wit: Wit, func: ExportFunction, cx: &mut Self::CallCx<'_>);
374377

375-
#[cfg(feature = "async")]
378+
#[cfg(feature = "async-runtime")]
376379
fn export_call_async(
377380
wit: Wit,
378381
func: ExportFunction,
379382
cx: Box<Self::CallCx<'static>>,
380383
) -> impl std::future::Future<Output = ()>;
381384

382-
#[cfg(not(feature = "async"))]
385+
#[cfg(feature = "async-raw")]
383386
fn export_async_start(wit: Wit, func: ExportFunction, cx: Box<Self::CallCx<'static>>) -> u32;
384387

385-
#[cfg(not(feature = "async"))]
388+
#[cfg(feature = "async-raw")]
386389
fn export_async_callback(event0: u32, event1: u32, event2: u32) -> u32;
387390

388391
fn export_finish(cx: Box<Self::CallCx<'_>>, func: ExportFunction) {
@@ -529,7 +532,7 @@ pub trait RawInterpreter: Interpreter {
529532
unsafe {
530533
let wit = Wit::from_raw(WIT_T);
531534
let func = wit.export_func(which);
532-
#[cfg(feature = "async")]
535+
#[cfg(feature = "async-runtime")]
533536
{
534537
wit_bindgen::rt::async_support::start_task(Self::export_call_async(
535538
wit,
@@ -538,21 +541,32 @@ pub trait RawInterpreter: Interpreter {
538541
))
539542
.cast_unsigned()
540543
}
541-
#[cfg(not(feature = "async"))]
544+
#[cfg(feature = "async-raw")]
542545
{
543546
Self::export_async_start(wit, func, Box::from_raw(cx.cast()))
544547
}
548+
#[cfg(not(any(feature = "async-runtime", feature = "async-raw")))]
549+
{
550+
_ = func;
551+
panic!("no async features enabled")
552+
}
545553
}
546554
}
547555

548556
unsafe fn raw_export_async_callback(a: u32, b: u32, c: u32, which: usize) -> u32 {
549557
debug_println!("export_async_callback({a:#x}, {b:#x}, {c:#x}, {which})");
550-
#[cfg(feature = "async")]
558+
#[cfg(feature = "async-runtime")]
551559
unsafe {
552560
wit_bindgen::rt::async_support::callback(a, b, c)
553561
}
554-
#[cfg(not(feature = "async"))]
555-
Self::export_async_callback(a, b, c)
562+
#[cfg(feature = "async-raw")]
563+
{
564+
Self::export_async_callback(a, b, c)
565+
}
566+
#[cfg(not(any(feature = "async-runtime", feature = "async-raw")))]
567+
{
568+
panic!("no async features enabled")
569+
}
556570
}
557571

558572
unsafe fn raw_export_finish(cx: *mut u8, which: usize) {

crates/wit-dylib/ffi/src/types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl ImportFunction {
393393
}
394394
}
395395

396-
#[cfg(not(feature = "async"))]
396+
#[cfg(feature = "async-raw")]
397397
pub unsafe fn call_import_async(&self, cx: &mut impl Call) -> Option<PendingAsyncImportCall> {
398398
use core::alloc::Layout;
399399

@@ -422,12 +422,12 @@ impl ImportFunction {
422422
}
423423
}
424424

425-
#[cfg(not(feature = "async"))]
425+
#[cfg(feature = "async-raw")]
426426
pub unsafe fn lift_import_async_result(&self, cx: &mut impl Call, buffer: *mut u8) {
427427
unsafe { self.ptr.async_lift_impl.unwrap()((&raw mut *cx).cast(), buffer.cast()) };
428428
}
429429

430-
#[cfg(feature = "async")]
430+
#[cfg(feature = "async-runtime")]
431431
pub async fn call_import_async(&self, cx: &mut impl Call) {
432432
use core::alloc::Layout;
433433
use wit_bindgen::rt::async_support::Subtask;

0 commit comments

Comments
 (0)