Why access subtyping is not allowed in type extensions?

I have tried this code and it confirmed my initial thought about subtyping relation.

trait HuntingSkills { fn test(&mut self); }
struct Animal {}
impl HuntingSkills for &mut Animal { fn test(&mut self) {} }
//wat?! &mut T is alowed but not others?
//impl &mut Animal {} fails!!


fn main() {
    let var1 = Animal {};
    //var1.test(); knows var1 is imut T
    let mut var2 = Animal {};
    //var2.test(); knows var2 is mut T
    let var4 = &var1;
    //var4.test(); //error compiler does distingusih between T and &T
    let mut var3 = &mut var2;
    var3.test(); //this works since explicit impl was defined for &mut T (Animal)
    println!("reached");
}

Meaning there is a flaw is lang's design in that it cannot address other sybtypes, which are:

  • T (used as type declaration and supertype for all subtypes)
  • mut T
  • &T
  • &mut T
  • dyn U
  • dyn mut U
  • dyn & U
  • dyn &mut U

So all these subtypes should be addresable with impl derictive. The most obviuos outcome is that you may lift nessecity for explicit self in methods

impl T { fn test(&mut self) {} } //old style
impl mut T { fn test() {} } //with subtyping