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

backon can't be used in along with &mut self #49

Closed
Xuanwo opened this issue Mar 1, 2023 · 2 comments · Fixed by #72
Closed

backon can't be used in along with &mut self #49

Xuanwo opened this issue Mar 1, 2023 · 2 comments · Fixed by #72

Comments

@Xuanwo
Copy link
Owner

Xuanwo commented Mar 1, 2023

async fn next(&mut self) -> Result<Option<Vec<oio::Entry>>> {
    { || self.inner.next() }
        .retry(&self.builder)
        .when(|e| e.is_temporary())
        .notify(|err, dur| {
            warn!(
                target: "opendal::service",
                "operation={} -> retry after {}s: error={:?}",
                Operation::Batch, dur.as_secs_f64(), err)
        })
        .map(|v| v.map_err(|e| e.set_persistent()))
        .await
}

Errors log:

error: captured variable cannot escape `FnMut` closure body
   --> src/layers/retry.rs:634:14
    |
633 |     async fn next(&mut self) -> Result<Option<Vec<oio::Entry>>> {
    |                    -------- variable defined here
634 |         { || self.inner.next() }
    |            - ----^^^^^^^^^^^^^
    |            | |
    |            | returns a reference to a captured variable which escapes the closure body
    |            | variable captured here
    |            inferred to be a `FnMut` closure
    |
    = note: `FnMut` closures only have access to their captured variables while they are executing...
    = note: ...therefore, they cannot allow references to captured variables to escape
@esemeniuc
Copy link

would love to see this fixed!

@Xuanwo
Copy link
Owner Author

Xuanwo commented Oct 20, 2023

would love to see this fixed!

I wish I can fix this too 🥹

No idea so far.

@Xuanwo Xuanwo closed this as completed in #72 Mar 8, 2024
Xuanwo added a commit that referenced this issue Mar 8, 2024
Close #49

```rust
use anyhow::anyhow;
use anyhow::Result;
use backon::ExponentialBuilder;
use backon::RetryableWithContext;

struct Test;

impl Test {
    async fn hello(&mut self) -> Result<usize> {
        Err(anyhow!("not retryable"))
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    let mut test = Test;

    // (Test, Result<usize>)
    let (_, result) = {
        |mut v: Test| async {
            let res = v.hello().await;
            (v, res)
        }
    }
    .retry(&ExponentialBuilder::default())
    .context(test)
    .await;

    Ok(())
}
```

Signed-off-by: Xuanwo <github@xuanwo.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants