rusttraitsfully-qualified-namingqualified-name

How can I qualify or hold to a higher precedence a method not provided by a trait?


I have an Add implementation that looks like this,

impl<T: Into<u64>> Add<T> for Sequence {
  type Output = Self;
  fn add(self, rhs: T) -> Self::Output { 
    let mut o = self.clone();
    o.myadd(rhs.into()).unwrap();
    o 
  } 
}

The function myadd returns a Result; This actually works fine. The problem is in the real world the method is Sequence.add() implemented on Sequence that I want the Add to call. If I rename myadd to add like this,

o.add(rhs.into()).unwrap();

Then it no longer compiles I get instead "error[E0599]: no method named unwrap found for struct sequence::Sequence in the current scope" which tells me that the add it's finding is not returning a Result, it's returning a Sequence which is not what I want. How can I qualify the trait in the call to add?


Solution

  • I was able to target add that I supplied under impl Sequence {} with the following,

    Sequence::add(r, rhs.into()).unwrap();
    

    You can also use

    Self::add(r, rhs.into()).unwrap();