This is a minimal example of my case.
I want to call method_a
and on specific failure call method_b
.
I can not figure out what I'm doing wrong.
Playground link: Rust Playground
use std::io::{ErrorKind, Result};
struct Foo {}
struct Bar<'f> {
foo: &'f mut Foo,
}
impl Foo {
fn method_a<'f>(&'f mut self) -> Result<Bar<'f>> {
Err(ErrorKind::WouldBlock.into())
}
fn method_b<'f>(&'f mut self) -> Result<Bar<'f>> {
Ok(Bar {
foo: self,
})
}
}
fn method_a_or_b<'f>(foo: &'f mut Foo) -> Result<Bar<'f>> {
match foo.method_a() {
Ok(bar) => Ok(bar),
Err(ref e) if e.kind() == ErrorKind::WouldBlock => foo.method_b(),
Err(e) => Err(e),
}
}
fn main() {
let mut foo = Foo {};
let _bar = method_a_or_b(&mut foo).unwrap();
}
The error message is the following:
error[E0499]: cannot borrow `*foo` as mutable more than once at a time
--> src/main.rs:24:60
|
21 | fn method_a_or_b<'f>(foo: &'f mut Foo) -> Result<Bar<'f>> {
| -- lifetime `'f` defined here
22 | match foo.method_a() {
| --- first mutable borrow occurs here
23 | Ok(bar) => Ok(bar),
| ------- returning this value requires that `*foo` is borrowed for `'f`
24 | Err(ref e) if e.kind() == ErrorKind::WouldBlock => foo.method_b(),
| ^^^ second mutable borrow occurs here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0499`.
error: could not compile `lifetime-issue`.
To learn more, run the command again with --verbose.