#Trying to `impl FromIterator<T> for Result<T, E>` where T is a local type

8 messages · Page 1 of 1 (latest)

timber basalt
#

I want to implement FromIterator for my local type Series. The problem is instantiation of Series is fallible, so FromIterator::from_iter must return Result.

My attempt:

impl<T: PartialOrd> FromIterator<T> for Result<Series<T>, Error<T>> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        Self::try_new(iter.into_iter().collect::<Vec<T>>())
    }
}

Results in:

error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)
  --> src/series.rs:58:6
   |
58 | impl<T: PartialOrd> FromIterator<T> for Result<Series<T>, Error<T>> {
   |      ^ type parameter `T` must be used as the type parameter for some local type
   |
   = note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
   = note: only traits defined in the current crate can be implemented for a type parameter

But T is being used in Series<T>, which is a local type. So I'm struggling to see what the problem is. Is it because the local type isn't "top-level"? (because Result is enclosing Series)

stark patio
#

Ye, that's why

#

impl Foreign<Local> for Foreign is allowed. impl Foreign for Foreign<Local> is not

#

Libstd could (and does) implement FromIterator for Result

timber basalt
#

Result only provides impl<A, E, V> FromIterator<Result<A, E>> for Result<V, E>

So am I just out of luck in this case? I guess I'll have to create my own trait

stark patio
#

What I'd do is not change the trait

#

It's change the type