Skip to content

Allow a minimum complete implementation to be specified for mutually recursive default methods #7771

Closed
@huonw

Description

@huonw

It should be possible to tell the compiler that at least one of a given set of default methods is required to be implemented, e.g.

#[requires(one_of(foo, bar), one_of(baz, qux))]
trait A {
   fn foo(&self) { self.bar() }
   fn bar(&self) { self.foo() }

   fn baz(&self) { self.qux() }
   fn qux(&self) { self.baz() }
}

impl A for int { // ok
   fn foo(&self) {}
   fn bar(&self) {}

   fn baz(&self) {} 
}

impl A for uint { // "requires at least one of foo or bar"
   fn baz(&self) {}
}

impl A for float { // "requires at least one of baz or qux"
   fn foo(&self) {}
}

This allows e.g. Eq to write eq and ne in terms of each other, without allowing the infinitely recursive impl Eq for Foo {}.

It could even allow saying "either foo or both bar and baz", e.g. #[requires(one_of(foo, all_of(bar, baz)))], and would hopefully warn if a non-default method was listed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-trait-systemArea: Trait system

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions