Noob in rust here, trying to implement the generic retry function and struggling how to resolve the lifetime error:
async fn retry<T, E, Fut, F>(mut f: F, client: &Client, max_retries: i32) -> Result<T, E>
where
Fut: Future<Output = Result<T, E>>,
F: FnMut(&Client) -> Fut,
{
let mut count = 0;
loop {
let result = f(client).await;
if result.is_ok() {
break result;
} else {
if count > max_retries {
break result;
}
count += 1;
}
}
}
and the error is
| |_________________^ returning this value requires that `'1` must outlive `'2`
error[E0515]: cannot return value referencing temporary value
The way I am calling the method is:
let lambda = &mut move |client: &Client| {
client
.download_object(
&GetObjectRequest {
bucket: some_bucket,
object: some_object,
..Default::default()
},
&Range::default(),
)
};
retry(lambda, &self.client, 5).await
I am not able to understand the return value of f(client).await
must outlive what exactly?
Full error:
error[E0621]: explicit lifetime required in the type of `client`
--> src/main.rs:35:22
|
28 | async fn retry<'a, T, E, Fut, F>(mut f: F, client: &Client, max_retries: i32) -> Result<T, E>
| ------- help: add explicit lifetime `'a` to the type of `client`: `&'a Client`
...
35 | let result = f(client).await;
| ^^^^^^^^^ lifetime `'a` required
error: lifetime may not live long enough
--> src/main.rs:68:13
|
67 | let lambda = &mut move |client: &Client| {
| - - return type of closure `impl Future<Output = Result<Vec<u8>, google_cloud_storage::http::Error>>` contains a lifetime `'2`
| |
| let's call the lifetime of this reference `'1`
68 | / client
69 | | .download_object(
70 | | &GetObjectRequest {
71 | | bucket: some_bucket,
... |
75 | | &Range::default(),
76 | | )
| |_________________^ returning this value requires that `'1` must outlive `'2`
error[E0515]: cannot return value referencing temporary value
--> src/main.rs:68:13
|
68 | / client
69 | | .download_object(
70 | | &GetObjectRequest {
| | ______________________-
71 | || bucket: some_bucket,
72 | || object: some_object,
73 | || ..Default::default()
74 | || },
| ||_____________________- temporary value created here
75 | | &Range::default(),
76 | | )
| |__________________^ returns a value referencing data owned by the current function
error[E0515]: cannot return value referencing temporary value
--> src/main.rs:68:13
|
68 | / client
69 | | .download_object(
70 | | &GetObjectRequest {
71 | | bucket: some_bucket,
... |
75 | | &Range::default(),
| | ---------------- temporary value created here
76 | | )
| |_________________^ returns a value referencing data owned by the current function