Why cant i implement From<postgres::Row> for Iterator<T>?

I am trying to implement From<postgres::Row> for my struct.
I have implemented From<&Row> and From but I cant find a way tom implement
it for an iterator which I need for the queries which return Vec

Below the code I tried which fails to compile

impl<T, O> From<T> for User
    T: Iterator,
    T::Item: From<Row>,
    O: Iterator<User>,
    fn from(r: T) -> O {
        r.map(|v| v.into())

The From trait expects your from() function to return a User. You are returning an O, which is an iterator of Users instead.

Depending on what you're trying to do, you could write this as a normal function that returns impl Iterator<Item=User>.

fn to_users<R>(rows: R) -> impl Iterator<Item = User>
  R: Iterator<Item = Row>,
  rows.map(|row| row.into())
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.