In the body of a trait
definition, allow final
as a function qualifier. final
appears before any other qualifier; the full qualifier order is subsequently final const async unsafe extern "ABI" fn
.
Final functions must have a default method body. Impl blocks for the trait must not provide a definition for any final functions.
Dyn compatible final methods are put into the trait's dyn vtable and dispatched virtually like with any other dyn compatible trait methods. This behaves similar to a supertrait which blanket implements methods for all sized implementers of the primary trait. Self: Sized
does not hold within the method body, as the impl may be used for other unsized kinds, like slices.
Dyn incompatible final methods are not put into the trait's dyn vtable; this is fundamentally impossible. However, they still are callable; a polymorphic version of the method is used for dyn Trait
, unlike other dyn compatible methods, which polymorphically dispatch to the monomorphic implementation.
Final functions which aren't methods (don't take self
in some form as the first parameter) are allowed, but make the trait dyn incompatible. Final functions called on a known type may be monomorphic or may be compiled polymorphically, as the compiler's detail.