# Ensure that at least one of multiple trait methods is overriden

**URL:** <https://users.rust-lang.org/t/ensure-that-at-least-one-of-multiple-trait-methods-is-overriden/56132>\
**Category:** help\
**Created:** [February 25, 2021, 1:42pm UTC](https://users.rust-lang.org/t/ensure-that-at-least-one-of-multiple-trait-methods-is-overriden/56132 "2021-02-25T13:42:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![raggy](https://avatars.discourse-cdn.com/v4/letter/r/b4bc9f/32.png) [@raggy](https://users.rust-lang.org/u/raggy)\
**Post date:** [February 25, 2021, 1:42pm UTC](https://users.rust-lang.org/t/ensure-that-at-least-one-of-multiple-trait-methods-is-overriden/56132/1 "2021-02-25T13:42:46Z")

</div>

So I have a trait Searchable for things that can be optimized using branch&bound search or similar methods. The trait has three methods that are relevant for this question:

```rust
trait Searchable{
  
  fn lower_bound(&self) -> Quality;

  fn is_solved(&self) -> bool {
    self.quality().is_some()
  }

  fn quality(&self) -> Option<Quality> {
    if self.is_solved() {
      Some(self.lower_bound())
    } else {
      None
  }
  // other methods
}

```

As you can see it is possible to express `is_solved` in terms of `quality` and the other way around.  
Now this is nice since the implementing type can override one or both of these methods and get the rest for free. But if both methods are left with the default impl we have a problem since they recurse forever.  
Does anyone know if there is any trick I can use to avoid the problem without giving up on the convenience of the default impls?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![cole-miller](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cole-miller/32/16722_2.png) [@cole-miller](https://users.rust-lang.org/u/cole-miller)\
**Post date:** [February 25, 2021, 2:47pm UTC](https://users.rust-lang.org/t/ensure-that-at-least-one-of-multiple-trait-methods-is-overriden/56132/2 "2021-02-25T14:47:13Z")

</div>

Personally I'd just pick one method (probably `is_solved`) to be required and one to be defaulted. I don't think any workaround is going to be simpler or more convenient than that, at least not when the methods are as straightforward as they are here.

---

<div class="post-metadata">

**Author:** ![raggy](https://avatars.discourse-cdn.com/v4/letter/r/b4bc9f/32.png) [@raggy](https://users.rust-lang.org/u/raggy)\
**Post date:** [February 25, 2021, 2:51pm UTC](https://users.rust-lang.org/t/ensure-that-at-least-one-of-multiple-trait-methods-is-overriden/56132/3 "2021-02-25T14:51:54Z")

</div>

You are right that is probably the way to go. I was just curious if there was some fancy solution I was unaware of.

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mbrubeck/32/648_2.png) [@mbrubeck](https://users.rust-lang.org/u/mbrubeck)\
**Post date:** [February 25, 2021, 5:13pm UTC](https://users.rust-lang.org/t/ensure-that-at-least-one-of-multiple-trait-methods-is-overriden/56132/4 "2021-02-25T17:13:43Z")

</div>

There's no built-in solution for this. See the links here for some past discussions/proposals:

[https://github.com/rust-lang/rfcs/issues/628](https://github.com/rust-lang/rfcs/issues/628)

---

<div class="post-metadata">

**Author:** ![raggy](https://avatars.discourse-cdn.com/v4/letter/r/b4bc9f/32.png) [@raggy](https://users.rust-lang.org/u/raggy)\
**Post date:** [February 26, 2021, 8:00am UTC](https://users.rust-lang.org/t/ensure-that-at-least-one-of-multiple-trait-methods-is-overriden/56132/5 "2021-02-26T08:00:35Z")

</div>

Thanks for the link. I knew I could not have been the first to encounter this but damn an issue from 2013 is straightup ancient for rust.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [May 27, 2021, 8:01am UTC](https://users.rust-lang.org/t/ensure-that-at-least-one-of-multiple-trait-methods-is-overriden/56132/6 "2021-05-27T08:01:03Z")

</div>

Closed on May 27, 2021, 8:01am UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
