Macro for generating iterators for custom container types?

⚓ rust    📅 2025-06-03    👤 surdeus    👁️ 1      

surdeus

My crate that I'm working on has a bunch of non-generic containers. Each container offers various ways to iterate through it, and according to the API Guidelines, each iterator method should return a type of the same name. Most of the time, I am simply iterating over a standard collection. So I end up writing a lot of this:

use delegate::delegate;

pub struct Processes {
    matched: Vec<Process>,
    ...
}

impl Processes {
    pub fn matched(&self) -> Matched {
        Matched(self.matched.iter())
    }
    ...
}

pub struct Matched<'a>(slice::Iter<'a, Process>);
impl<'a> Iterator for Matched<'a> {
    type Item = &'a Process;
    delegate! {
        to self.0 {
            fn next(&mut self) -> Option<Self::Item>;
            fn size_hint(&self) -> (usize, Option<usize>);
        }
    }
}

I am already using the delegate macro but it still feels like too much boilerplate. Is there a macro that can help even more in this situation?

3 posts - 3 participants

Read full topic

🏷️ rust_feed