Skip to content
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

Upgrade to Rust 2024 and AsyncFnMut #3

Merged
merged 1 commit into from
Feb 26, 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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
[package]
name = "named-retry"
version = "0.3.0"
version = "0.4.0"
authors = ["Eric Zhang <[email protected]>"]
license = "MIT"
description = "A simple utility for retrying fallible asynchronous operations."
repository = "https://github.com/modal-labs/named-retry"
documentation = "https://docs.rs/named-retry"
keywords = ["retry", "async"]
categories = ["development-tools::debugging", "asynchronous", "network-programming"]
categories = [
"development-tools::debugging",
"asynchronous",
"network-programming",
]
readme = "README.md"
edition = "2021"
edition = "2024"
rust-version = "1.85"

[dependencies]
tokio = { version = "1.24.2", features = ["time"] }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ let retry = Retry::new("test")
.delay_factor(2.0)
.jitter(true);

let result = retry.run(|| async { Ok::<_, ()>("done!") }).await;
let result = retry.run(async || { Ok::<_, ()>("done!") }).await;
assert_eq!(result, Ok("done!"));
```
47 changes: 21 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Utilities for retrying falliable, asynchronous operations.

use std::fmt::Debug;
use std::future::Future;
use std::time::Duration;

use tokio::time;
Expand Down Expand Up @@ -80,10 +79,10 @@ impl Retry {
///
/// Panics if the number of attempts is set to `0`, or the base delay is
/// incorrectly set to a negative duration.
pub async fn run<T, E: Debug, Fut>(self, mut func: impl FnMut() -> Fut) -> Result<T, E>
where
Fut: Future<Output = Result<T, E>>,
{
pub async fn run<T, E: Debug>(
self,
mut func: impl AsyncFnMut() -> Result<T, E>,
) -> Result<T, E> {
assert!(self.attempts > 0, "attempts must be greater than 0");
assert!(
self.base_delay >= Duration::ZERO && self.delay_factor >= 0.0,
Expand Down Expand Up @@ -118,16 +117,16 @@ mod tests {
async fn zero_retry_attempts() {
let _ = Retry::new("test")
.attempts(0)
.run(|| async { Ok::<_, std::io::Error>(()) })
.run(async || Ok::<_, std::io::Error>(()))
.await;
}

#[tokio::test]
async fn successful_retry() {
let mut count = 0;
let task = Retry::new("test").run(|| {
let task = Retry::new("test").run(async || {
count += 1;
async { Ok::<_, std::io::Error>(()) }
Ok::<_, std::io::Error>(())
});
let result = task.await;
assert_eq!(count, 1);
Expand All @@ -138,9 +137,9 @@ mod tests {
async fn failed_retry() {
let mut count = 0;
let retry = Retry::new("test");
let task = retry.run(|| {
let task = retry.run(async || {
count += 1;
async { Err::<(), ()>(()) }
Err::<(), ()>(())
});
let result = task.await;
assert_eq!(count, retry.attempts);
Expand All @@ -157,15 +156,13 @@ mod tests {
.attempts(5)
.base_delay(Duration::from_secs(1))
.delay_factor(2.0)
.run(|| {
.run(async || {
count += 1;
async {
println!("elapsed = {:?}", start.elapsed());
if start.elapsed() < Duration::from_secs(5) {
Err::<(), ()>(())
} else {
Ok(())
}
println!("elapsed = {:?}", start.elapsed());
if start.elapsed() < Duration::from_secs(5) {
Err::<(), ()>(())
} else {
Ok(())
}
});
let result = task.await;
Expand All @@ -184,15 +181,13 @@ mod tests {
.base_delay(Duration::from_millis(100))
.delay_factor(10.0)
.jitter(true)
.run(|| {
.run(async || {
count += 1;
async {
println!("elapsed = {:?}", start.elapsed());
if start.elapsed() < Duration::from_millis(500) {
Err::<(), ()>(())
} else {
Ok(())
}
println!("elapsed = {:?}", start.elapsed());
if start.elapsed() < Duration::from_millis(500) {
Err::<(), ()>(())
} else {
Ok(())
}
});
let result = task.await;
Expand Down
Loading