Skip to content

Commit 7208f7b

Browse files
authored
Switch Subtask trait to taking &self (#1390)
This enables having a subtask dispatched based on in-memory variable information as opposed to requiring static information up-front. I'm intending to use this for `wit-dylib` over in wasm-tools to prove out some async bits. This'll eventually happen for futures/streams most likely as well, but for now it's just subtasks. This also shouldn't affect runtime code generated for this crate from before since the types used are all zero-sized and will get elided at runtime. Thus this is intended to enable more use cases without altering existing ones.
1 parent 545dc8d commit 7208f7b

File tree

6 files changed

+127
-99
lines changed

6 files changed

+127
-99
lines changed

crates/guest-rust/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -872,10 +872,12 @@ pub mod examples;
872872
#[doc(hidden)]
873873
pub mod rt;
874874

875+
#[cfg(feature = "async")]
876+
#[allow(deprecated)]
877+
pub use rt::async_support::backpressure_set;
875878
#[cfg(feature = "async")]
876879
pub use rt::async_support::{
877-
backpressure_dec, backpressure_inc, backpressure_set, block_on, spawn, yield_async,
878-
yield_blocking, AbiBuffer, FutureRead, FutureReader, FutureWrite, FutureWriteCancel,
879-
FutureWriteError, FutureWriter, StreamRead, StreamReader, StreamResult, StreamWrite,
880-
StreamWriter,
880+
backpressure_dec, backpressure_inc, block_on, spawn, yield_async, yield_blocking, AbiBuffer,
881+
FutureRead, FutureReader, FutureWrite, FutureWriteCancel, FutureWriteError, FutureWriter,
882+
StreamRead, StreamReader, StreamResult, StreamWrite, StreamWriter,
881883
};

crates/guest-rust/src/rt/async_support/future_support.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ impl<T> FutureWriter<T> {
269269
/// In such a situation the operation can be retried at a future date.
270270
pub fn write(self, value: T) -> FutureWrite<T> {
271271
FutureWrite {
272-
op: WaitableOperation::new((self, value)),
272+
op: WaitableOperation::new(FutureWriteOp(marker::PhantomData), (self, value)),
273273
}
274274
}
275275
}
@@ -339,7 +339,7 @@ where
339339
type Result = (WriteComplete<T>, FutureWriter<T>);
340340
type Cancel = FutureWriteCancel<T>;
341341

342-
fn start((writer, value): Self::Start) -> (u32, Self::InProgress) {
342+
fn start(&self, (writer, value): Self::Start) -> (u32, Self::InProgress) {
343343
// TODO: it should be safe to store the lower-destination in
344344
// `WaitableOperation` using `Pin` memory and such, but that would
345345
// require some type-level trickery to get a correctly-sized value
@@ -361,11 +361,12 @@ where
361361
(code, (writer, cleanup))
362362
}
363363

364-
fn start_cancelled((writer, value): Self::Start) -> Self::Cancel {
364+
fn start_cancelled(&self, (writer, value): Self::Start) -> Self::Cancel {
365365
FutureWriteCancel::Cancelled(value, writer)
366366
}
367367

368368
fn in_progress_update(
369+
&self,
369370
(mut writer, cleanup): Self::InProgress,
370371
code: u32,
371372
) -> Result<Self::Result, Self::InProgress> {
@@ -425,19 +426,19 @@ where
425426
}
426427
}
427428

428-
fn in_progress_waitable((writer, _): &Self::InProgress) -> u32 {
429+
fn in_progress_waitable(&self, (writer, _): &Self::InProgress) -> u32 {
429430
writer.handle
430431
}
431432

432-
fn in_progress_cancel((writer, _): &Self::InProgress) -> u32 {
433+
fn in_progress_cancel(&self, (writer, _): &Self::InProgress) -> u32 {
433434
// SAFETY: we're managing `writer` and all the various operational bits,
434435
// so this relies on `WaitableOperation` being safe.
435436
let code = unsafe { (writer.vtable.cancel_write)(writer.handle) };
436437
rtdebug!("future.cancel-write({}) = {code:#x}", writer.handle);
437438
code
438439
}
439440

440-
fn result_into_cancel((result, writer): Self::Result) -> Self::Cancel {
441+
fn result_into_cancel(&self, (result, writer): Self::Result) -> Self::Cancel {
441442
match result {
442443
// The value was actually sent, meaning we can't yield back the
443444
// future nor the value.
@@ -581,7 +582,7 @@ impl<T> IntoFuture for FutureReader<T> {
581582
/// written to the writable end of this `future`.
582583
fn into_future(self) -> Self::IntoFuture {
583584
FutureRead {
584-
op: WaitableOperation::new(self),
585+
op: WaitableOperation::new(FutureReadOp(marker::PhantomData), self),
585586
}
586587
}
587588
}
@@ -622,7 +623,7 @@ where
622623
type Result = (ReadComplete<T>, FutureReader<T>);
623624
type Cancel = Result<T, FutureReader<T>>;
624625

625-
fn start(reader: Self::Start) -> (u32, Self::InProgress) {
626+
fn start(&self, reader: Self::Start) -> (u32, Self::InProgress) {
626627
let (ptr, cleanup) = Cleanup::new(reader.vtable.layout);
627628
// SAFETY: `ptr` is allocated with `vtable.layout` and should be
628629
// safe to use here. Its lifetime for the async operation is hinged on
@@ -632,11 +633,12 @@ where
632633
(code, (reader, cleanup))
633634
}
634635

635-
fn start_cancelled(state: Self::Start) -> Self::Cancel {
636+
fn start_cancelled(&self, state: Self::Start) -> Self::Cancel {
636637
Err(state)
637638
}
638639

639640
fn in_progress_update(
641+
&self,
640642
(reader, cleanup): Self::InProgress,
641643
code: u32,
642644
) -> Result<Self::Result, Self::InProgress> {
@@ -667,19 +669,19 @@ where
667669
}
668670
}
669671

670-
fn in_progress_waitable((reader, _): &Self::InProgress) -> u32 {
672+
fn in_progress_waitable(&self, (reader, _): &Self::InProgress) -> u32 {
671673
reader.handle()
672674
}
673675

674-
fn in_progress_cancel((reader, _): &Self::InProgress) -> u32 {
676+
fn in_progress_cancel(&self, (reader, _): &Self::InProgress) -> u32 {
675677
// SAFETY: we're managing `reader` and all the various operational bits,
676678
// so this relies on `WaitableOperation` being safe.
677679
let code = unsafe { (reader.vtable.cancel_read)(reader.handle()) };
678680
rtdebug!("future.cancel-read({}) = {code:#x}", reader.handle());
679681
code
680682
}
681683

682-
fn result_into_cancel((value, reader): Self::Result) -> Self::Cancel {
684+
fn result_into_cancel(&self, (value, reader): Self::Result) -> Self::Cancel {
683685
match value {
684686
// The value was actually read, so thread that through here.
685687
ReadComplete::Value(value) => Ok(value),

crates/guest-rust/src/rt/async_support/stream_support.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<T> StreamWriter<T> {
143143
/// instead of `Vec<T>`.
144144
pub fn write_buf(&mut self, values: AbiBuffer<T>) -> StreamWrite<'_, T> {
145145
StreamWrite {
146-
op: WaitableOperation::new((self, values)),
146+
op: WaitableOperation::new(StreamWriteOp(marker::PhantomData), (self, values)),
147147
}
148148
}
149149

@@ -243,7 +243,7 @@ where
243243
type Result = (StreamResult, AbiBuffer<T>);
244244
type Cancel = (StreamResult, AbiBuffer<T>);
245245

246-
fn start((writer, buf): Self::Start) -> (u32, Self::InProgress) {
246+
fn start(&self, (writer, buf): Self::Start) -> (u32, Self::InProgress) {
247247
if writer.done {
248248
return (DROPPED, (writer, buf));
249249
}
@@ -259,11 +259,12 @@ where
259259
(code, (writer, buf))
260260
}
261261

262-
fn start_cancelled((_writer, buf): Self::Start) -> Self::Cancel {
262+
fn start_cancelled(&self, (_writer, buf): Self::Start) -> Self::Cancel {
263263
(StreamResult::Cancelled, buf)
264264
}
265265

266266
fn in_progress_update(
267+
&self,
267268
(writer, mut buf): Self::InProgress,
268269
code: u32,
269270
) -> Result<Self::Result, Self::InProgress> {
@@ -284,19 +285,19 @@ where
284285
}
285286
}
286287

287-
fn in_progress_waitable((writer, _): &Self::InProgress) -> u32 {
288+
fn in_progress_waitable(&self, (writer, _): &Self::InProgress) -> u32 {
288289
writer.handle
289290
}
290291

291-
fn in_progress_cancel((writer, _): &Self::InProgress) -> u32 {
292+
fn in_progress_cancel(&self, (writer, _): &Self::InProgress) -> u32 {
292293
// SAFETY: we're managing `writer` and all the various operational bits,
293294
// so this relies on `WaitableOperation` being safe.
294295
let code = unsafe { (writer.vtable.cancel_write)(writer.handle) };
295296
rtdebug!("stream.cancel-write({}) = {code:#x}", writer.handle);
296297
code
297298
}
298299

299-
fn result_into_cancel(result: Self::Result) -> Self::Cancel {
300+
fn result_into_cancel(&self, result: Self::Result) -> Self::Cancel {
300301
result
301302
}
302303
}
@@ -391,7 +392,7 @@ impl<T> StreamReader<T> {
391392
/// used.
392393
pub fn read(&mut self, buf: Vec<T>) -> StreamRead<'_, T> {
393394
StreamRead {
394-
op: WaitableOperation::new((self, buf)),
395+
op: WaitableOperation::new(StreamReadOp(marker::PhantomData), (self, buf)),
395396
}
396397
}
397398

@@ -459,7 +460,7 @@ where
459460
type Result = (StreamResult, Vec<T>);
460461
type Cancel = (StreamResult, Vec<T>);
461462

462-
fn start((reader, mut buf): Self::Start) -> (u32, Self::InProgress) {
463+
fn start(&self, (reader, mut buf): Self::Start) -> (u32, Self::InProgress) {
463464
if reader.done {
464465
return (DROPPED, (reader, buf, None));
465466
}
@@ -492,11 +493,12 @@ where
492493
(code, (reader, buf, cleanup))
493494
}
494495

495-
fn start_cancelled((_, buf): Self::Start) -> Self::Cancel {
496+
fn start_cancelled(&self, (_, buf): Self::Start) -> Self::Cancel {
496497
(StreamResult::Cancelled, buf)
497498
}
498499

499500
fn in_progress_update(
501+
&self,
500502
(reader, mut buf, cleanup): Self::InProgress,
501503
code: u32,
502504
) -> Result<Self::Result, Self::InProgress> {
@@ -553,19 +555,19 @@ where
553555
}
554556
}
555557

556-
fn in_progress_waitable((reader, ..): &Self::InProgress) -> u32 {
558+
fn in_progress_waitable(&self, (reader, ..): &Self::InProgress) -> u32 {
557559
reader.handle()
558560
}
559561

560-
fn in_progress_cancel((reader, ..): &Self::InProgress) -> u32 {
562+
fn in_progress_cancel(&self, (reader, ..): &Self::InProgress) -> u32 {
561563
// SAFETY: we're managing `reader` and all the various operational bits,
562564
// so this relies on `WaitableOperation` being safe.
563565
let code = unsafe { (reader.vtable.cancel_read)(reader.handle()) };
564566
rtdebug!("stream.cancel-read({}) = {code:#x}", reader.handle());
565567
code
566568
}
567569

568-
fn result_into_cancel(result: Self::Result) -> Self::Cancel {
570+
fn result_into_cancel(&self, result: Self::Result) -> Self::Cancel {
569571
result
570572
}
571573
}

0 commit comments

Comments
 (0)