Skip to content

Commit

Permalink
docs: Add more examples
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo committed Mar 8, 2024
1 parent d295d85 commit 86b5109
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
//! 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<String> {
//! 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<String> {
//! 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)]
Expand Down

0 comments on commit 86b5109

Please sign in to comment.