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

docs: Add a new example for retry inside &mut self function #118

Merged
merged 1 commit into from
Aug 27, 2024
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
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