简体   繁体   中英

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

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() {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM