Skip to content

Use Waker::noop from Rust 1.85 #10804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions crates/c-api/src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::num::NonZeroU64;
use std::ops::Range;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::task::{Context, Poll, Waker};
use std::{ptr, str};
use wasmtime::{
AsContextMut, Func, Instance, Result, RootScope, StackCreator, StackMemory, Trap, Val,
Expand Down Expand Up @@ -199,8 +199,11 @@ pub extern "C" fn wasmtime_call_future_delete(_future: Box<wasmtime_call_future_

#[unsafe(no_mangle)]
pub extern "C" fn wasmtime_call_future_poll(future: &mut wasmtime_call_future_t) -> bool {
let w = futures::task::noop_waker_ref();
match future.underlying.as_mut().poll(&mut Context::from_waker(w)) {
match future
.underlying
.as_mut()
.poll(&mut Context::from_waker(Waker::noop()))
{
Poll::Ready(()) => true,
Poll::Pending => false,
}
Expand Down
4 changes: 2 additions & 2 deletions crates/fuzzing/src/oracles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst};
use std::sync::{Arc, Condvar, Mutex};
use std::task::{Context, Poll};
use std::task::{Context, Poll, Waker};
use std::time::{Duration, Instant};
use wasmtime::*;
use wasmtime_wast::WastContext;
Expand Down Expand Up @@ -1320,7 +1320,7 @@ pub fn call_async(wasm: &[u8], config: &generators::Config, mut poll_amts: &[u32

fn run<F: Future>(future: F) -> F::Output {
let mut f = Box::pin(future);
let mut cx = Context::from_waker(futures::task::noop_waker_ref());
let mut cx = Context::from_waker(Waker::noop());
loop {
match f.as_mut().poll(&mut cx) {
Poll::Ready(val) => break val,
Expand Down
3 changes: 2 additions & 1 deletion crates/wasi-tls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ fn try_lock_for_stream<TlsWriter>(
#[cfg(test)]
mod tests {
use super::*;
use std::task::Waker;
use tokio::sync::oneshot;

#[tokio::test]
Expand All @@ -689,7 +690,7 @@ mod tests {

let mut fut = future_streams.ready();

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

//cancel the readiness check
Expand Down
6 changes: 3 additions & 3 deletions crates/wasi/src/p2/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::mem;
use std::net::{Shutdown, SocketAddr};
use std::pin::Pin;
use std::sync::Arc;
use std::task::Poll;
use std::task::{Poll, Waker};
use tokio::sync::Mutex;

/// The state of a TCP socket.
Expand Down Expand Up @@ -265,7 +265,7 @@ impl TcpSocket {
let result = match previous_state {
TcpState::ConnectReady(result) => result,
TcpState::Connecting(mut future) => {
let mut cx = std::task::Context::from_waker(futures::task::noop_waker_ref());
let mut cx = std::task::Context::from_waker(Waker::noop());
match with_ambient_tokio_runtime(|| future.as_mut().poll(&mut cx)) {
Poll::Ready(result) => result,
Poll::Pending => {
Expand Down Expand Up @@ -369,7 +369,7 @@ impl TcpSocket {
let result = match pending_accept.take() {
Some(result) => result,
None => {
let mut cx = std::task::Context::from_waker(futures::task::noop_waker_ref());
let mut cx = std::task::Context::from_waker(Waker::noop());
match with_ambient_tokio_runtime(|| listener.poll_accept(&mut cx))
.map_ok(|(stream, _)| stream)
{
Expand Down
4 changes: 2 additions & 2 deletions crates/wasi/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use std::future::Future;
use std::pin::Pin;
use std::sync::LazyLock;
use std::task::{Context, Poll};
use std::task::{Context, Poll, Waker};

pub(crate) static RUNTIME: LazyLock<tokio::runtime::Runtime> = LazyLock::new(|| {
tokio::runtime::Builder::new_multi_thread()
Expand Down Expand Up @@ -181,7 +181,7 @@ pub fn poll_noop<F>(future: Pin<&mut F>) -> Option<F::Output>
where
F: Future,
{
let mut task = Context::from_waker(futures::task::noop_waker_ref());
let mut task = Context::from_waker(Waker::noop());
match future.poll(&mut task) {
Poll::Ready(result) => Some(result),
Poll::Pending => None,
Expand Down
21 changes: 1 addition & 20 deletions examples/min-platform/embedding/src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,24 +416,6 @@ impl ExecutorInner {
}
}

// Yanked from core::task::wake, which is unfortunately still unstable :/
fn noop_waker() -> Waker {
use core::task::{RawWaker, RawWakerVTable};
const VTABLE: RawWakerVTable = RawWakerVTable::new(
// Cloning just returns a new no-op raw waker
|_| RAW,
// `wake` does nothing
|_| {},
// `wake_by_ref` does nothing
|_| {},
// Dropping does nothing as we don't allocate anything
|_| {},
);
const RAW: RawWaker = RawWaker::new(core::ptr::null(), &VTABLE);

unsafe { Waker::from_raw(RAW) }
}

fn block_on<R>(clock: Clock, f: impl Future<Output = Result<R>> + Send + 'static) -> Result<R> {
// Guard against nested invocations
if EXECUTOR.0.borrow_mut().is_some() {
Expand All @@ -443,8 +425,7 @@ fn block_on<R>(clock: Clock, f: impl Future<Output = Result<R>> + Send + 'static
*EXECUTOR.0.borrow_mut() = Some(Executor(executor.0.clone()));

// No special waker needed for this executor.
let waker = noop_waker();
let mut cx = Context::from_waker(&waker);
let mut cx = Context::from_waker(Waker::noop());
let mut f = core::pin::pin!(f);

// Drive the Future to completion in the following loop
Expand Down
13 changes: 3 additions & 10 deletions tests/all/async_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::{anyhow, bail};
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
use std::task::{Context, Poll, Waker};
use wasmtime::*;

fn async_store() -> Store<()> {
Expand Down Expand Up @@ -206,7 +206,7 @@ async fn suspend_while_suspending() {
let mut future = Box::pin(async_thunk.call_async(&mut caller, &[], &mut []));
let poll = future
.as_mut()
.poll(&mut Context::from_waker(&noop_waker()));
.poll(&mut Context::from_waker(Waker::noop()));
assert!(poll.is_ready());
Ok(())
});
Expand Down Expand Up @@ -593,7 +593,7 @@ async fn resume_separate_thread3() {
let mut future = Box::pin(f);
let poll = future
.as_mut()
.poll(&mut Context::from_waker(&noop_waker()));
.poll(&mut Context::from_waker(Waker::noop()));
assert!(poll.is_pending());

// ... so at this point our call into wasm is suspended. The call into
Expand Down Expand Up @@ -780,13 +780,6 @@ where
}
}

fn noop_waker() -> Waker {
const VTABLE: RawWakerVTable =
RawWakerVTable::new(|ptr| RawWaker::new(ptr, &VTABLE), |_| {}, |_| {}, |_| {});
const RAW: RawWaker = RawWaker::new(0 as *const (), &VTABLE);
unsafe { Waker::from_raw(RAW) }
}

#[tokio::test]
async fn non_stacky_async_activations() -> Result<()> {
let mut config = Config::new();
Expand Down