Skip to content

Commit d41b0a7

Browse files
authored
Use Waker::noop from Rust 1.85 (#10804)
Now possible after #10785
1 parent b4f041f commit d41b0a7

File tree

7 files changed

+19
-41
lines changed

7 files changed

+19
-41
lines changed

crates/c-api/src/async.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::num::NonZeroU64;
55
use std::ops::Range;
66
use std::pin::Pin;
77
use std::sync::Arc;
8-
use std::task::{Context, Poll};
8+
use std::task::{Context, Poll, Waker};
99
use std::{ptr, str};
1010
use wasmtime::{
1111
AsContextMut, Func, Instance, Result, RootScope, StackCreator, StackMemory, Trap, Val,
@@ -199,8 +199,11 @@ pub extern "C" fn wasmtime_call_future_delete(_future: Box<wasmtime_call_future_
199199

200200
#[unsafe(no_mangle)]
201201
pub extern "C" fn wasmtime_call_future_poll(future: &mut wasmtime_call_future_t) -> bool {
202-
let w = futures::task::noop_waker_ref();
203-
match future.underlying.as_mut().poll(&mut Context::from_waker(w)) {
202+
match future
203+
.underlying
204+
.as_mut()
205+
.poll(&mut Context::from_waker(Waker::noop()))
206+
{
204207
Poll::Ready(()) => true,
205208
Poll::Pending => false,
206209
}

crates/fuzzing/src/oracles.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use std::future::Future;
2929
use std::pin::Pin;
3030
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst};
3131
use std::sync::{Arc, Condvar, Mutex};
32-
use std::task::{Context, Poll};
32+
use std::task::{Context, Poll, Waker};
3333
use std::time::{Duration, Instant};
3434
use wasmtime::*;
3535
use wasmtime_wast::WastContext;
@@ -1320,7 +1320,7 @@ pub fn call_async(wasm: &[u8], config: &generators::Config, mut poll_amts: &[u32
13201320

13211321
fn run<F: Future>(future: F) -> F::Output {
13221322
let mut f = Box::pin(future);
1323-
let mut cx = Context::from_waker(futures::task::noop_waker_ref());
1323+
let mut cx = Context::from_waker(Waker::noop());
13241324
loop {
13251325
match f.as_mut().poll(&mut cx) {
13261326
Poll::Ready(val) => break val,

crates/wasi-tls/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ fn try_lock_for_stream<TlsWriter>(
678678
#[cfg(test)]
679679
mod tests {
680680
use super::*;
681+
use std::task::Waker;
681682
use tokio::sync::oneshot;
682683

683684
#[tokio::test]
@@ -691,7 +692,7 @@ mod tests {
691692

692693
let mut fut = future_streams.ready();
693694

694-
let mut cx = std::task::Context::from_waker(futures::task::noop_waker_ref());
695+
let mut cx = std::task::Context::from_waker(Waker::noop());
695696
assert!(fut.as_mut().poll(&mut cx).is_pending());
696697

697698
//cancel the readiness check

crates/wasi/src/p2/tcp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::mem;
1818
use std::net::{Shutdown, SocketAddr};
1919
use std::pin::Pin;
2020
use std::sync::Arc;
21-
use std::task::Poll;
21+
use std::task::{Poll, Waker};
2222
use tokio::sync::Mutex;
2323

2424
/// The state of a TCP socket.
@@ -265,7 +265,7 @@ impl TcpSocket {
265265
let result = match previous_state {
266266
TcpState::ConnectReady(result) => result,
267267
TcpState::Connecting(mut future) => {
268-
let mut cx = std::task::Context::from_waker(futures::task::noop_waker_ref());
268+
let mut cx = std::task::Context::from_waker(Waker::noop());
269269
match with_ambient_tokio_runtime(|| future.as_mut().poll(&mut cx)) {
270270
Poll::Ready(result) => result,
271271
Poll::Pending => {
@@ -369,7 +369,7 @@ impl TcpSocket {
369369
let result = match pending_accept.take() {
370370
Some(result) => result,
371371
None => {
372-
let mut cx = std::task::Context::from_waker(futures::task::noop_waker_ref());
372+
let mut cx = std::task::Context::from_waker(Waker::noop());
373373
match with_ambient_tokio_runtime(|| listener.poll_accept(&mut cx))
374374
.map_ok(|(stream, _)| stream)
375375
{

crates/wasi/src/runtime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use std::future::Future;
2323
use std::pin::Pin;
2424
use std::sync::LazyLock;
25-
use std::task::{Context, Poll};
25+
use std::task::{Context, Poll, Waker};
2626

2727
pub(crate) static RUNTIME: LazyLock<tokio::runtime::Runtime> = LazyLock::new(|| {
2828
tokio::runtime::Builder::new_multi_thread()
@@ -181,7 +181,7 @@ pub fn poll_noop<F>(future: Pin<&mut F>) -> Option<F::Output>
181181
where
182182
F: Future,
183183
{
184-
let mut task = Context::from_waker(futures::task::noop_waker_ref());
184+
let mut task = Context::from_waker(Waker::noop());
185185
match future.poll(&mut task) {
186186
Poll::Ready(result) => Some(result),
187187
Poll::Pending => None,

examples/min-platform/embedding/src/wasi.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -416,24 +416,6 @@ impl ExecutorInner {
416416
}
417417
}
418418

419-
// Yanked from core::task::wake, which is unfortunately still unstable :/
420-
fn noop_waker() -> Waker {
421-
use core::task::{RawWaker, RawWakerVTable};
422-
const VTABLE: RawWakerVTable = RawWakerVTable::new(
423-
// Cloning just returns a new no-op raw waker
424-
|_| RAW,
425-
// `wake` does nothing
426-
|_| {},
427-
// `wake_by_ref` does nothing
428-
|_| {},
429-
// Dropping does nothing as we don't allocate anything
430-
|_| {},
431-
);
432-
const RAW: RawWaker = RawWaker::new(core::ptr::null(), &VTABLE);
433-
434-
unsafe { Waker::from_raw(RAW) }
435-
}
436-
437419
fn block_on<R>(clock: Clock, f: impl Future<Output = Result<R>> + Send + 'static) -> Result<R> {
438420
// Guard against nested invocations
439421
if EXECUTOR.0.borrow_mut().is_some() {
@@ -443,8 +425,7 @@ fn block_on<R>(clock: Clock, f: impl Future<Output = Result<R>> + Send + 'static
443425
*EXECUTOR.0.borrow_mut() = Some(Executor(executor.0.clone()));
444426

445427
// No special waker needed for this executor.
446-
let waker = noop_waker();
447-
let mut cx = Context::from_waker(&waker);
428+
let mut cx = Context::from_waker(Waker::noop());
448429
let mut f = core::pin::pin!(f);
449430

450431
// Drive the Future to completion in the following loop

tests/all/async_functions.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use anyhow::{anyhow, bail};
44
use std::future::Future;
55
use std::pin::Pin;
66
use std::sync::{Arc, Mutex};
7-
use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
7+
use std::task::{Context, Poll, Waker};
88
use wasmtime::*;
99

1010
fn async_store() -> Store<()> {
@@ -206,7 +206,7 @@ async fn suspend_while_suspending() {
206206
let mut future = Box::pin(async_thunk.call_async(&mut caller, &[], &mut []));
207207
let poll = future
208208
.as_mut()
209-
.poll(&mut Context::from_waker(&noop_waker()));
209+
.poll(&mut Context::from_waker(Waker::noop()));
210210
assert!(poll.is_ready());
211211
Ok(())
212212
});
@@ -593,7 +593,7 @@ async fn resume_separate_thread3() {
593593
let mut future = Box::pin(f);
594594
let poll = future
595595
.as_mut()
596-
.poll(&mut Context::from_waker(&noop_waker()));
596+
.poll(&mut Context::from_waker(Waker::noop()));
597597
assert!(poll.is_pending());
598598

599599
// ... so at this point our call into wasm is suspended. The call into
@@ -780,13 +780,6 @@ where
780780
}
781781
}
782782

783-
fn noop_waker() -> Waker {
784-
const VTABLE: RawWakerVTable =
785-
RawWakerVTable::new(|ptr| RawWaker::new(ptr, &VTABLE), |_| {}, |_| {}, |_| {});
786-
const RAW: RawWaker = RawWaker::new(0 as *const (), &VTABLE);
787-
unsafe { Waker::from_raw(RAW) }
788-
}
789-
790783
#[tokio::test]
791784
async fn non_stacky_async_activations() -> Result<()> {
792785
let mut config = Config::new();

0 commit comments

Comments
 (0)