Why this fires¶
An impl Trait for Type (or the compact-struct nested impl Trait { … }) is
missing one or more required methods of the trait. A trait method is
required unless the trait gives it a default body. Without the method, operator
and method dispatch would fail at runtime (e.g. < on an Ord type whose
cmp was never implemented).
struct Money { cents: int }
impl core::cmp::Ord for Money { }
// ^ error[TE825]: incomplete impl of trait `core::cmp::Ord` for `Money`:
// missing required method(s) `cmp`The same applies to a bare trait name and to the compact-struct nested form:
impl Eq for Tag { }
// ^ error[TE825]: ... missing required method(s) `eq`
struct Money(cents: int) {
impl core::cmp::Ord { } // error[TE825]: ... missing required method(s) `cmp`
}Fix it¶
Implement every required method of the trait:
impl core::cmp::Ord for Money {
fn cmp(self, other: Money) -> int {
if self.cents < other.cents { 0 - 1 } else { self.cents > other.cents && 1 || 0 }
}
}Required methods of the common std traits: Ord→cmp, Eq→eq,
Add→add, Sub→sub, Mul→mul, Div→div, Mod→mod_, Neg→neg,
Display→to_string, Hash→hash.
Notes¶
- A trait method with a default body is not required — an empty impl of a default-only trait is complete.
- Methods marked
@native/@plugin, impls inside declaration modules (.d.zolo), and impls in scope of ause plugin …::*glob are exempt (their bodies may be supplied externally). - This checks for missing required methods only; extra methods not declared by the trait are not flagged.