Iterator Filtering trait method

I'm working on implementing https://tc39.es/ecma402/ as traits and I can't figure out how to handle supported_locales_of method.

Such method should accept a list of Locale values, and return a filtered version of that list narrowed down based on some match criteria (availability of the locale).

I could do this with slices or lists, but I wanted to try to be a bit more precise and use an iterator. So, I want to accept an iterator, filter it, and return an iterator - kind of like a stream.

I currently have something like this:

// Mock of a Locale
#[derive(Clone)]
pub struct Locale {
    language: String
}

/* TRAIT */

pub trait ListFormatType {
    type Iter: Iterator<Item = Locale>;

    fn supported_locales_of<I>(input: I) -> Self::Iter
        where
            I: IntoIterator<Item = Locale>;
}

struct ListFormat {}

impl ListFormatType for ListFormat {
    type Iter = Iterator<Item = Locale>;

    fn supported_locales_of<I>(input: I) -> Self::Iter
        where
            I: IntoIterator<Item = Locale> {
        input.into_iter().filter(|loc| AVAILABLE_LOCALES.contains(loc))
    }
}

but that doesn't work because I need GAT or a concrete type in the implementation.
What concrete type should I use or is there another way to handle such scenario?

To define this method as a trait method, you would have to define your own struct and implement iterator on it. Trait methods are not as powerful as struct methods in this regard.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.