I have this code:
use core::any::Any;
pub enum Either<L, R> {
Left(L),
Right(R),
}
impl<'a, L: 'static, R: 'static> Either<&'a mut L, &'a mut R> {
pub fn downcast_mut_from(from: &'a mut dyn Any) -> Option<Self> {
if let Some(left) = from.downcast_mut() {
Some(Self::Left(left))
} else if let Some(right) = from.downcast_mut() {
Some(Self::Right(right))
} else {
None
}
}
}
which fails with the following borrow check error:
error[E0499]: cannot borrow `*from` as mutable more than once at a time
--> src/lib.rs:12:37
|
8 | impl<'a, L: 'static, R: 'static> Either<&'a mut L, &'a mut R> {
| -- lifetime `'a` defined here
9 | pub fn downcast_mut_from(from: &'a mut dyn Any) -> Option<Self> {
10 | if let Some(left) = from.downcast_mut() {
| ------------------- first mutable borrow occurs here
11 | Some(Self::Left(left))
| ---------------- argument requires that `*from` is borrowed for `'a`
12 | } else if let Some(right) = from.downcast_mut() {
| ^^^^^^^^^^^^^^^^^^^ second mutable borrow occurs here
For more information about this error, try `rustc --explain E0499`.
I can work around this by checking the type with Any::is
first and then only calling downcast_mut
once, but I'm curious if there's any way to actually be able to call downcast_mut
multiple times in this situation?