Repository: diesel-rs/diesel
File: diesel/src/sqlite/connection/statement_iterator.rs (Rust)

Code before:
            })
            .collect::<QueryResult<_>>()?;

        self.column_indices = Some(column_indices);
        Ok(())
    }
}

impl<'a, T> Iterator for NamedStatementIterator<'a, T>
where
    T: QueryableByName<Sqlite>,
{
    type Item = QueryResult<T>;

    fn next(&mut self) -> Option<Self::Item> {
        let row = match self.stmt.step() {
            Ok(row) => row,
            Err(e) => return Some(Err(e)),
        };
        if self.column_indices.is_none() {
            if let Err(e) = self.populate_column_indices() {
                return Some(Err(e));
            }
        }
        row.map(|row| {
            let row = row.into_named(self.column_indices.as_ref().expect("it's there because we populated it above"));
            T::build(&row).map_err(DeserializationError)
        })
    }
}


Review comment (suggestion):
So this seems to not break memory safety specifically because this call to `into_named` enforces downgrading the lifetime of `'a` in `HashMap<&'a str, usize>` to the lifetime of `self`.
Maybe we could explicit it there in order to contain why this works closer? (It looks that currently it wouldn't be hard to change the signature of `into_named`, see it compile and call it a day.)
```rust
impl<'a, T> Iterator for NamedStatementIterator<'a, T>
where
    T: QueryableByName<Sqlite>,
{
    type Item = QueryResult<T>;

    fn next<'s>(&'s mut self) -> Option<Self::Item> {
        let row = match self.stmt.step() {
            Ok(row) => row,
            Err(e) => return Some(Err(e)),
        };
        if self.column_indices.is_none() {
            if let Err(e) = self.populate_column_indices() {
                return Some(Err(e));
            }
        }
        row.map(|row| {
            // explicitly downgrade the lifetime to a correct one
            let column_indices: &'s HashMap<&'s str, usize> = self.column_indices.as_ref().expect("it's there because we populated it above");
            let row = row.into_named(column_indices);
            T::build(&row).map_err(DeserializationError)
        })
    }
}
```

Additionally it looks like we could add a doc comment on `column_indices` of to specify that `'a` is not precisely correct in that context.

Code after:
                        .to_str()
                        .map_err(|e| DeserializationError(e.into()))?;
                    Ok((column, i))
                })
            })
            .collect::<QueryResult<_>>()?;

        self.column_indices = Some(column_indices);
        Ok(())
    }
}

impl<'a, T> Iterator for NamedStatementIterator<'a, T>
where
    T: QueryableByName<Sqlite>,
{
    type Item = QueryResult<T>;

    fn next(&mut self) -> Option<Self::Item> {
        let row = match self.stmt.step() {
            Ok(row) => row,
            Err(e) => return Some(Err(e)),
        };
        if self.column_indices.is_none() {
            if let Err(e) = self.populate_column_indices() {
                return Some(Err(e));
            }
        }
        row.map(|row| {
            let row = row.into_named(
                self.column_indices
                    .as_ref()
                    .expect("it's there because we populated it above"),
            );
            T::build(&row).map_err(DeserializationError)
        })
    }
}