diff --git a/src/lib.rs b/src/lib.rs index 8bc147a..0e93d4a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -68,6 +68,92 @@ //! Ok(()) //! } //! ``` +//! +//! Retry functions with args. +//! +//! ```no_run +//! use anyhow::Result; +//! use backon::ExponentialBuilder; +//! use backon::Retryable; +//! +//! async fn fetch(url: &str) -> Result { +//! Ok(reqwest::get(url).await?.text().await?) +//! } +//! +//! #[tokio::main(flavor = "current_thread")] +//! async fn main() -> Result<()> { +//! let content = (|| async { fetch("https://www.rust-lang.org").await }) +//! .retry(&ExponentialBuilder::default()) +//! .when(|e| e.to_string() == "retryable") +//! .await?; +//! +//! println!("fetch succeeded: {}", content); +//! Ok(()) +//! } +//! ``` +//! +//! Retry functions with receiver `&self`. +//! +//! ```no_run +//! use anyhow::Result; +//! use backon::ExponentialBuilder; +//! use backon::Retryable; +//! +//! struct Test; +//! +//! impl Test { +//! async fn fetch(&self, url: &str) -> Result { +//! Ok(reqwest::get(url).await?.text().await?) +//! } +//! } +//! +//! #[tokio::main(flavor = "current_thread")] +//! async fn main() -> Result<()> { +//! let test = Test; +//! let content = (|| async { test.fetch("https://www.rust-lang.org").await }) +//! .retry(&ExponentialBuilder::default()) +//! .when(|e| e.to_string() == "retryable") +//! .await?; +//! +//! println!("fetch succeeded: {}", content); +//! Ok(()) +//! } +//! ``` +//! +//! Retry functions with receiver `&mut self`. +//! +//! ```no_run +//! use anyhow::Result; +//! use backon::ExponentialBuilder; +//! use backon::RetryableWithContext; +//! +//! struct Test; +//! +//! impl Test { +//! async fn fetch(&mut self, url: &str) -> Result { +//! Ok(reqwest::get(url).await?.text().await?) +//! } +//! } +//! +//! #[tokio::main(flavor = "current_thread")] +//! async fn main() -> Result<()> { +//! let test = Test; +//! +//! let (_, result) = (|mut v: Test| async { +//! let res = v.fetch("https://www.rust-lang.org").await; +//! // Return input context back. +//! (v, res) +//! }) +//! .retry(&ExponentialBuilder::default()) +//! // Passing context in. +//! .context(test) +//! .when(|e| e.to_string() == "retryable") +//! .await; +//! +//! println!("fetch succeeded: {}", result.unwrap()); +//! Ok(()) +//! } +//! ``` #![deny(missing_docs)] #![deny(unused_qualifications)]