From 4a28de483a21ccfb7aea3012a6a80e39d28bc236 Mon Sep 17 00:00:00 2001 From: Siyuan Yan <44753941+Madoshakalaka@users.noreply.github.com> Date: Wed, 26 Mar 2025 16:02:55 +0900 Subject: [PATCH] Only rewrite async closure for rust version before 1.85.0 (#3831) * use_prepared_state with unit type is unlikely usage --------- Co-authored-by: Matt "Siyuan" Yan --- packages/yew-macro/Cargo.toml | 2 +- packages/yew-macro/src/use_prepared_state.rs | 6 +++--- packages/yew/src/functional/hooks/use_prepared_state/mod.rs | 6 +----- tools/benchmark-ssr/src/main.rs | 3 ++- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/packages/yew-macro/Cargo.toml b/packages/yew-macro/Cargo.toml index 240bee23e..556de28a0 100644 --- a/packages/yew-macro/Cargo.toml +++ b/packages/yew-macro/Cargo.toml @@ -22,10 +22,10 @@ quote = "1" syn = { version = "2", features = ["full", "extra-traits", "visit-mut"] } once_cell = "1" prettyplease = "0.2" +rustversion = "1" # testing [dev-dependencies] -rustversion = "1" trybuild = "1" yew = { path = "../yew" } diff --git a/packages/yew-macro/src/use_prepared_state.rs b/packages/yew-macro/src/use_prepared_state.rs index ec1a87712..7c221ce1f 100644 --- a/packages/yew-macro/src/use_prepared_state.rs +++ b/packages/yew-macro/src/use_prepared_state.rs @@ -58,8 +58,8 @@ impl Parse for PreparedState { } impl PreparedState { - // Async closure is not stable, so we rewrite it to closure + async block - #[cfg(not(nightly_yew))] + // Async closure was not stable, so we rewrite it to closure + async block + #[rustversion::before(1.85)] pub fn rewrite_to_closure_with_async_block(&self) -> ExprClosure { use proc_macro2::Span; use syn::parse_quote; @@ -95,7 +95,7 @@ impl PreparedState { closure } - #[cfg(nightly_yew)] + #[rustversion::since(1.85)] pub fn rewrite_to_closure_with_async_block(&self) -> ExprClosure { self.closure.clone() } diff --git a/packages/yew/src/functional/hooks/use_prepared_state/mod.rs b/packages/yew/src/functional/hooks/use_prepared_state/mod.rs index c611be624..fe4a85916 100644 --- a/packages/yew/src/functional/hooks/use_prepared_state/mod.rs +++ b/packages/yew/src/functional/hooks/use_prepared_state/mod.rs @@ -47,7 +47,7 @@ pub use feat_ssr::*; /// # { todo!() } /// ``` /// -/// The first argument can also be an [async closure](https://github.com/rust-lang/rust/issues/62290). +/// The first argument can also be an async closure /// /// `let state = use_prepared_state!(async |deps| -> ReturnType { ... }, deps)?;` /// @@ -85,10 +85,6 @@ pub use feat_ssr::*; /// You MUST denote the return type of the closure with `|deps| -> ReturnType { ... }`. This /// type is used during client side rendering to deserialize the state prepared on the server /// side. -/// -/// Whilst async closure is an unstable feature, the procedural macro will rewrite this to a -/// closure that returns an async block automatically. You can use this hook with async closure -/// in stable Rust. pub use use_prepared_state_macro as use_prepared_state; // With SSR. #[doc(hidden)] diff --git a/tools/benchmark-ssr/src/main.rs b/tools/benchmark-ssr/src/main.rs index cf0ae56e5..d2c46de98 100644 --- a/tools/benchmark-ssr/src/main.rs +++ b/tools/benchmark-ssr/src/main.rs @@ -141,8 +141,9 @@ async fn bench_concurrent_task() -> Duration { #[function_component] fn Comp() -> HtmlResult { - let _state = use_prepared_state!((), async move |_| -> () { + let _state = use_prepared_state!((), async move |_| -> usize { sleep(Duration::from_secs(1)).await; + 42 })?; Ok(Html::default())