2
votes

I'm having issues getting the lifetimes correct (again...) for a trait implementation.

There's a postgres Row which I'd like to make convertible into my own structure like this:

impl<'a> From<&'a Row> for Video {
    fn from(row: &Row) -> Video {
        Video {
            video_id: row.get("video_id"),
            ...
        }
    }
}

But I get an error like this:

src/entities.rs:46:19: 46:22 error: wrong number of lifetime parameters: expected 1, found 0 [E0107]
src/entities.rs:46 impl<'a> From<&'a Row> for Video {
                                     ^~~

which doesn't make sense to me - the lifetime param is right there. What's missing?

1

1 Answers

4
votes

The lifetime is on the reference to the Row, but not the Row itself. To make a reproduction easier, I defined something that looks like a Row:

struct Foo<'a> {
    s: &'a str,
} 

When we impl, we need to do this:

impl<'a> From<&'a Foo<'a>> for String {
    fn from(row: &Foo) -> String {
        row.s.to_string()
    }
}

Does that make sense? If you didn't have the reference:

impl<'a> From<Foo<'a>> for String {