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 <mattsy1999@gmail.com>
This commit is contained in:
Siyuan Yan 2025-03-26 16:02:55 +09:00 committed by GitHub
parent e5b2cf3611
commit 4a28de483a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 7 additions and 10 deletions

View File

@ -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" }

View File

@ -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()
}

View File

@ -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)]

View File

@ -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())