Skip to content

Commit 2edb104

Browse files
committed
split wit-dylib async feature into high- and low-level variations
- `async-high-level`: integrates with `std::future` by way of `wit_bindgen::rt` - `async-low-level`: minimal layer over raw bindings; does not use `std::future` or `wit_bindgen` Note that enabling both is equivalent to enabling only `async-low-level`. Signed-off-by: Joel Dice <[email protected]>
1 parent 416c5da commit 2edb104

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-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-low-level = []
20+
async-high-level = ['dep:wit-bindgen']

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -372,17 +372,17 @@ pub trait Interpreter: 'static {
372372

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

375-
#[cfg(feature = "async")]
375+
#[cfg(all(feature = "async-high-level", not(feature = "async-low-level")))]
376376
fn export_call_async(
377377
wit: Wit,
378378
func: ExportFunction,
379379
cx: Box<Self::CallCx<'static>>,
380380
) -> impl std::future::Future<Output = ()>;
381381

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

385-
#[cfg(not(feature = "async"))]
385+
#[cfg(feature = "async-low-level")]
386386
fn export_async_callback(event0: u32, event1: u32, event2: u32) -> u32;
387387

388388
fn export_finish(cx: Box<Self::CallCx<'_>>, func: ExportFunction) {
@@ -529,7 +529,7 @@ pub trait RawInterpreter: Interpreter {
529529
unsafe {
530530
let wit = Wit::from_raw(WIT_T);
531531
let func = wit.export_func(which);
532-
#[cfg(feature = "async")]
532+
#[cfg(all(feature = "async-high-level", not(feature = "async-low-level")))]
533533
{
534534
wit_bindgen::rt::async_support::start_task(Self::export_call_async(
535535
wit,
@@ -538,21 +538,32 @@ pub trait RawInterpreter: Interpreter {
538538
))
539539
.cast_unsigned()
540540
}
541-
#[cfg(not(feature = "async"))]
541+
#[cfg(feature = "async-low-level")]
542542
{
543543
Self::export_async_start(wit, func, Box::from_raw(cx.cast()))
544544
}
545+
#[cfg(not(any(feature = "async-high-level", feature = "async-low-level")))]
546+
{
547+
_ = func;
548+
panic!("no async features enabled")
549+
}
545550
}
546551
}
547552

548553
unsafe fn raw_export_async_callback(a: u32, b: u32, c: u32, which: usize) -> u32 {
549554
debug_println!("export_async_callback({a:#x}, {b:#x}, {c:#x}, {which})");
550-
#[cfg(feature = "async")]
555+
#[cfg(all(feature = "async-high-level", not(feature = "async-low-level")))]
551556
unsafe {
552557
wit_bindgen::rt::async_support::callback(a, b, c)
553558
}
554-
#[cfg(not(feature = "async"))]
555-
Self::export_async_callback(a, b, c)
559+
#[cfg(feature = "async-low-level")]
560+
{
561+
Self::export_async_callback(a, b, c)
562+
}
563+
#[cfg(not(any(feature = "async-high-level", feature = "async-low-level")))]
564+
{
565+
panic!("no async features enabled")
566+
}
556567
}
557568

558569
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-low-level")]
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-low-level")]
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(all(feature = "async-high-level", not(feature = "async-low-level")))]
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)