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

痴心易碎 提交于 2019-12-18 09:26:08

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/36645452/variable-does-not-live-long-enough-when-storing-a-csvdecodedrecords-iterator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!