But the above code fails to meet the lifetime requirements (playground link):
error[E0499]: cannot borrow `file` as mutable more than once at a time
--> src/lib.rs:12:18
|
4 | async fn write_forever<'a, Fut>(
| -- lifetime `'a` defined here
...
12 | write_fn(&mut file).await?;
| ---------^^^^^^^^^-
| | |
| | `file` was mutably borrowed here in the previous iteration of the loop
| argument requires that `file` is borrowed for `'a`
For more information about this error, try `rustc --explain E0499`.
I couldn't see the problem here, since although the returned Fut contains a borrow to the File, the future object should be automatically dropped after its completion, so the borrow should also be released.
Is it possible to make the above code compile, or am I missing something important?
Unfortunately the above helper trait only works with real functions. If you try to use a closure, the compiler will throw a type inference error because it's too stupid.