Subtype operations and base type invokations

Subtype operations

Given the Fields in traits proposal, there could be a way of doing descendant trait or opaque type test with a trait object (dyn T).

Relatedly, it is already allowed for Rust programs to downcast trait objects to a contravariant type, although in a limited form currently.


// Type test
is_subtype!(v, T);

// Type conversions
let v: Rc<dyn TR1> = v;
let v: Rc<dyn TR2> = v.try_into().unwrap();
let v: Rc<Opaque> = v.try_into().unwrap();

Base type invokations

  • The super.f() or super(o).f() expression could be used to inherit code from either an implemented trait or a super trait.
    • There is room for optimization for such feature, such as using a separate function for common code.

Problems with the above

Traits, as used with super expression and provided methods may occasionally duplicate substantial amounts of code, as provided methods pass through monomorphy for every implementor of such trait.