retry_macro

Crates.ioretry_macro
lib.rsretry_macro
version0.3.0
created_at2022-11-16 17:12:51.232655+00
updated_at2025-04-01 21:17:13.193199+00
descriptionA set of declarative macros which retries executing a function upon failure
homepage
repositoryhttps://github.com/bwintertkb/retry_macro
max_upload_size
id716593
size35,874
Bartek Winter (bwintertkb)

documentation

https://docs.rs/retry_macro/0.1.0/retry_macro/

README

retry_macro

A library which provides macros that automatically re-execute both synchronous and asynchronous (tokio/async-std with sleep) functions upon failure.

Feature flags

  • Enable features = ["tokio"] if you want to use the retry_async_sleep macro.
  • Enable features = ["async-std"] if you want to use the retry_async_sleep macro with async-std.

Examples

Here are two simple examples using both the synchronous and asynchronous macros. Note that all function inputs must be bounded to an identifier (variable).

Synchronous

use retry_macro::{retry, retry_sleep, RetryError};

fn can_fail(input: &str) -> Result<i32, std::num::ParseIntError> {
    input.parse::<i32>()
}

fn main() {
    // Retry the can_fail function 5 times with the input "not_a_number"
    let var = "not_a_number";
    let res = retry!(5, can_fail, var);
    assert!(res.is_err());
    assert_eq!(res.unwrap_err().retries.len(), 5);

    // Retry the can_fail function 5 times with the input "not_a_number", sleep for 100 milliseconds between retries
    let var = "not_a_number";
    let res = retry_sleep!(5, 100, can_fail, var);
    assert!(res.is_err());
    assert_eq!(res.unwrap_err().retries.len(), 5);
}

Asynchronous (with tokio)

use retry_macro::{retry_async, retry_async_sleep, RetryError};

async fn can_fail_async(input: &str) -> Result<i32, std::num::ParseIntError> {
    tokio::time::sleep(tokio::time::Duration::from_millis(1)).await;
    input.parse::<i32>()
}

#[tokio::main]
async fn main() {
    // Retry the can_fail function 5 times with the input "not_a_number"
    let var = "not_a_number";
    let res = retry_async!(5, can_fail_async, var);
    assert!(res.is_err());
    assert_eq!(res.unwrap_err().retries.len(), 5);

    // Retry the can_fail function 5 times with the input "not_a_number", sleep for 100 milliseconds between retries
    let var = "not_a_number";
    let res = retry_async_sleep!(5, 100, can_fail_async, var);
    assert!(res.is_err());
    assert_eq!(res.unwrap_err().retries.len(), 5);
}

Asynchronous (with async-std)

use retry_macro::{retry_async, retry_async_sleep, RetryError};

async fn can_fail_async(input: &str) -> Result<i32, std::num::ParseIntError> {
    async_std::task::sleep(std::time::Duration::from_millis(1)).await;
    input.parse::<i32>()
}

#[async_std::main]
async fn main() {
    // Retry the can_fail function 5 times with the input "not_a_number"
    let var = "not_a_number";
    let res = retry_async!(5, can_fail_async, var);
    assert!(res.is_err());
    assert_eq!(res.unwrap_err().retries.len(), 5);

    // Retry the can_fail function 5 times with the input "not_a_number", sleep for 100 milliseconds between retries
    let var = "not_a_number";
    let res = retry_async_sleep!(5, 100, can_fail_async, var);
    assert!(res.is_err());
    assert_eq!(res.unwrap_err().retries.len(), 5);
}

License

retry_macro is distributed under the MIT and Apache-2.0 licenses.

Commit count: 13

cargo fmt