According to the documentation, Reader::decode
is defined as:
fn decode<'a, D: Decodable>(&'a mut self) -> DecodedRecords<'a, R, D>
That is reader.decode()
cannot outlive reader
(because of 'a
).
And with this declaration:
struct CSVBarSource {
records: csv::DecodedRecords<'static, std::fs::File, BarRecord>,
// ^~~~~~~
}
reader
would need a 'static
lifetime, that is it would need to live forever, which it does not hence the error you get “reader
does not live long enough”.
You should store reader
directly in CSVBarSource
:
struct CSVBarSource {
reader: csv::Reader<std::fs::File>,
}
And call decode
only as needed.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…