Skip to content

Preview of async closure#183

Closed
Xuanwo wants to merge 3 commits intomainfrom
async-closure-preview
Closed

Preview of async closure#183
Xuanwo wants to merge 3 commits intomainfrom
async-closure-preview

Conversation

@Xuanwo
Copy link
Copy Markdown
Owner

@Xuanwo Xuanwo commented Mar 6, 2025

This PR serves as an async closure preview in the backon. After this PR, people can use backon on async closure directly like:

#[test]
async fn test_async_closure_retry_with_not_retryable_error() {
    let error_times = Mutex::new(0);

    let f = async || {
        let mut x = error_times.lock().await;
        *x += 1;
        Err::<(), anyhow::Error>(anyhow::anyhow!("not retryable"))
    };

    let backoff = ExponentialBuilder::default().with_min_delay(Duration::from_millis(1));
    let result = f
        .retry(backoff)
        // Only retry If error message is `retryable`
        .when(|e| e.to_string() == "retryable")
        .await;

    assert!(result.is_err());
    assert_eq!("not retryable", result.unwrap_err().to_string());
    // `f` always returns error "not retryable", so it should be executed
    // only once.
    assert_eq!(*error_times.lock().await, 1);
}

However, this PR relies on unstable features: unboxed_closures and async_fn_traits. So users will need nightly to use it.

Xuanwo added 2 commits March 6, 2025 13:14
Signed-off-by: Xuanwo <github@xuanwo.io>
Signed-off-by: Xuanwo <github@xuanwo.io>
Xuanwo added a commit that referenced this pull request Oct 18, 2025
This PR will bump backon's edition to 2024 and MSRV to 1.85 so that we
can support more native features like
#183

---

**This PR was primarily authored with Codex using GPT-5-Codex and then
hand-reviewed by me. I AM responsible for every change made in this PR.
I aimed to keep it aligned with our goals, though I may have missed
minor issues. Please flag anything that feels off, I'll fix it
quickly.**

Signed-off-by: Xuanwo <github@xuanwo.io>
@Xuanwo
Copy link
Copy Markdown
Owner Author

Xuanwo commented Oct 18, 2025

Ooops, we still need #![feature(async_fn_traits)] after rust 1.85

@Xuanwo Xuanwo closed this Oct 18, 2025
@Xuanwo Xuanwo mentioned this pull request Oct 18, 2025
@Xuanwo Xuanwo deleted the async-closure-preview branch October 18, 2025 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants