Search code examples
rustassociated-types

Traits with default methods that depend on a bound of an associated type


I can't figure out a way to make this code compile with --cfg on_trait:

trait DigitCollection: Sized {
    type Iter: Iterator<Item = u8>;
    fn digit_iter(self) -> Self::Iter;

    #[cfg(on_trait)]
    fn digit_sum(self) -> u32 {
        self.digit_iter()
            .map(|digit: u8| digit as u32)
            .fold(0, |sum, digit| sum + digit)
    }
}

#[cfg(not(on_trait))]
fn digit_sum<T: DigitCollection>(collection: T) -> u32 {
    collection.digit_iter()
        .map(|digit: u8| digit as u32)
        .fold(0, |sum, digit| sum + digit)
}

fn main() {
}

With on_trait this fails with:

trait.rs:7:14: 7:26 error: type annotations required: cannot resolve `<<Self as DigitCollection>::Iter as core::iter::Iterator>::Item == u8` [E0284]
trait.rs:7         self.digit_iter()
                        ^~~~~~~~~~~~
error: aborting due to previous error

Without on_trait, it compiles just fine. Note that the not(on_trait) variant only differs by the fact that it is a free function instead of a default method.


Edit: I opened an issue on this: rust-lang/rust#22036


Solution

  • This code now compiles as desired:

    trait DigitCollection: Sized {
        type Iter: Iterator<Item = u8>;
        fn digit_iter(self) -> Self::Iter;
    
        fn digit_sum(self) -> u32 {
            self.digit_iter()
                .map(|digit: u8| digit as u32)
                .fold(0, |sum, digit| sum + digit)
        }
    }
    
    fn main() {}