Testcase:
use std::ops::*;
struct S<'a, T:FnMut() + 'static + ?Sized>(&'a mut T);
impl<'a, T:?Sized + FnMut() + 'static> DerefMut for S<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 }
}
impl<'a, T:?Sized + FnMut() + 'static> Deref for S<'a, T> {
type Target = FnMut() + 'a;
fn deref(&self) -> &Self::Target { &self.0 }
}
fn main() {
let mut f = ||{};
let mut s = S(&mut f);
s();
}
Yields:
<anon>:13:5: 13:6 error: cannot borrow immutable borrowed content as mutable
<anon>:13 s();
^
Auto-deref should be picking DerefMut here, not Deref.
I just ran into this problem too, here's a shorter example:
use std::cell::RefCell;
use std::rc::Rc;
struct Foo(Rc<RefCell<FnMut()>>);
fn main() {
let a = Foo(Rc::new(RefCell::new(||{
println!("Hello, world!");
})));
a.0.borrow_mut()();
}
And playpen link: https://is.gd/KQJ9ij
Further reduced:
use std::cell::RefCell;
use std::rc::Rc;
fn main() {
let a: Rc<RefCell<FnMut()>> = Rc::new(RefCell::new(||{}));
a.borrow_mut()();
}
Another example from Stack Overflow:
use std::ops::{Deref, DerefMut};
struct Foo;
impl Deref for Foo {
type Target = FnMut() + 'static;
fn deref(&self) -> &Self::Target {
unimplemented!()
}
}
impl DerefMut for Foo {
fn deref_mut(&mut self) -> &mut Self::Target {
unimplemented!()
}
}
fn main() {
let mut t = Foo;
t();
}
And again experienced on Stack Overflow
Just got this today, with what is almost identical to Mark-Simulacrum's reduced version.
I've also run into this issue, with this example:
use std::sync::Mutex;
struct FunctionIcon {
get_icon: Mutex<Box<dyn FnMut() -> u32>>,
}
impl FunctionIcon {
fn get_icon(&self) -> impl '_ + std::ops::DerefMut<Target=Box<dyn FnMut() -> u32>> {
self.get_icon.lock().unwrap()
}
fn load_icon(&self) {
let mut get_icon = self.get_icon();
let rgba_icon = (*get_icon)();
}
}
and this error:
error[E0596]: cannot borrow data in a dereference of `impl std::ops::DerefMut` as mutable
--> src/lib.rs:14:25
|
14 | let rgba_icon = (*get_icon)();
| ^^^^^^^^^^^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `impl std::ops::DerefMut`
Most helpful comment
Further reduced: