Search code examples
rustiteratortraits

Оverflow evaluating the requirement, Rust Traits


I am trying to write a trait which requires an inner type Iter. And Iter should implements Iterator. I am getting such error "overflow evaluating the requirement <str as SomeTrait>::Iter == _" and can't understand what it wants. Can anybody help to understand ?)

pub trait SomeIter { 
   type Iter where Self::Iter: Iterator;
}

struct NumberIter(u32, u8);

impl Iterator for NumberIter {
    type Item = u8;

    fn next(&mut self) -> Option<Self::Item> {
        if self.1 == 32 || (self.0 >> self.1) == 0 {
            None
        } else {
            let ret = ((self.0 >> self.1) as u8) & 0xFF;
            self.1 += 8;
            Some(ret)
        }
    }
}

impl SomeTrait for str {
    type Iter = NumberIter;
}

The compiler advised reading some examples in https://doc.rust-lang.org/error-index.html#E0275 but there weren't similar ones.


Solution

  • The problem is the resolution over the where Self::Iter part (because it is recursive), where you just really need to constrain the type:

    pub trait SomeIter { 
       type Iter: Iterator;
    }
    

    Playground