Skip to content

Commit

Permalink
docs: Add a new example for retry inside &mut self function (#118)
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo authored Aug 27, 2024
1 parent 0af3ab2 commit ce472cd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/docs/examples/inside_mut_self.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Retry an async function inside `&mut self` functions.

```rust
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?)
}

async fn run(&mut self) -> Result<String> {
let content = (|| async { self.fetch("https://www.rust-lang.org").await })
.retry(ExponentialBuilder::default())
.when(|e| e.to_string() == "retryable")
.await?;
Ok(content)
}
}
```
3 changes: 3 additions & 0 deletions src/docs/examples/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ pub mod basic {}
#[doc = include_str!("closure.md")]
pub mod closure {}

#[doc = include_str!("inside_mut_self.md")]
pub mod inside_mut_self {}

#[doc = include_str!("sqlx.md")]
pub mod sqlx {}

Expand Down
1 change: 1 addition & 0 deletions src/docs/examples/with_self.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Retry an async function which takes `&self` as receiver.
}
}


#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
let test = Test;
Expand Down

0 comments on commit ce472cd

Please sign in to comment.