In the following code sample I'm curious how the borrow_mut()
calls are routed to the correct object. Is it because of the Deref
trait and deref coercion?
use std::rc::Rc;
use std::cell::RefCell;
#[derive(Debug)]
struct Foo {
}
impl Foo {
fn take_foo_by_self(&mut self) {
println!("took {:?} by self", self)
}
}
#[derive(Debug)]
struct ContainsFoo {
foo: Rc<RefCell<Box<Foo>>>,
}
fn take_foo(foo: &mut Foo) {
println!("took {:?} explicitly", foo)
}
fn main() {
let f = Foo { };
let rc = Rc::new(RefCell::new(Box::new(f)));
let cf0 = ContainsFoo { foo: rc.clone() };
let cf1 = ContainsFoo { foo: rc.clone() };
take_foo(&mut cf0.foo.borrow_mut()); // <--- How does this function call work?
cf1.foo.borrow_mut().take_foo_by_self(); // <--- and this?
}
Rust playground link: Rust Playground