Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

rust - variable does not live long enough when storing a csv::DecodedRecords iterator

I'm trying to create an iterator trait that provides a specific type of resource, so I can implement multiple source types. I'd like to create a source for reading from a CSV file, a binary etc..

I'm using the rust-csv library for deserializing CSV data:

#[derive(RustcDecodable)]
struct BarRecord {
    bar: u32
}

trait BarSource : Iterator {}

struct CSVBarSource {
    records: csv::DecodedRecords<'static, std::fs::File, BarRecord>,
}

impl CSVBarSource {
    pub fn new(path: String) -> Option<CSVBarSource> {
        match csv::Reader::from_file(path) {
            Ok(reader) => Some(CSVBarSource { records: reader.decode() }),
            Err(_) => None
        }
    }
}

impl Iterator for CSVBarSource {
    type Item = BarRecord;

    fn next(&mut self) -> Option<BarRecord> {
        match self.records.next() {
            Some(Ok(e)) => Some(e),
            _ => None
        }
    }
}

I cannot seem to store a reference to the DecodedRecords iterator returned by the CSV reader due to lifetime issues:

error: reader does not live long enough

How can I store a reference to the decoded records iterator and what am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...