How deref work?

If the deref target and source both have the same method,, then what's the precedence?

Here is the example from rust book:

use crate::List::{Cons, Nil};
use std::rc::Rc;
use std::cell::RefCell;

fn main() {
    let value = Rc::new(RefCell::new(5));

    let a = Rc::new(Cons(Rc::clone(&value), Rc::new(Nil)));

    let b = Cons(Rc::new(RefCell::new(6)), Rc::clone(&a));
    let c = Cons(Rc::new(RefCell::new(10)), Rc::clone(&a));

    *value.borrow_mut() += 10;

    println!("a after = {:?}", a);
    println!("b after = {:?}", b);
    println!("c after = {:?}", c);
}

Rc has blanket implementation borror_mut, and RefCell has explicit borrow_mut method.
Then which one for the compiler to choose?
Here it wound use the RefCell one. It seems that the deref target is precedence.

But it's strange that when I try to test it myself, it gives different result.

use std::cell::RefCell;
use std::ops::Deref;
use std::rc::Rc;

struct A;
impl A {
    fn echo(&self) {
        println!("echo Axx");
    }
}

struct B {
    a: A,
}

impl B {}

impl Deref for B {
    type Target = A;

    #[inline(always)]
    fn deref(&self) -> &A {
        &self.a
    }
}

trait C {
    fn echo(&self);
}

impl C for B {
    fn echo(&self) {
        println!("fefefe");
    }
}

let a = A {};
let b = B { a: a };
b.echo();

It uses the method of B, instead of A.

Why?

The compiler only lookup Deref Target's method if current type doesn't have a method with corresponding name. But in your first example, the .borrow_mut() is defined on the trait BorrowMut, and you can't call trait's method without use it to import to current scope.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.