1
我想创建一个提供特定类型的资源的迭代器特征,所以我可以实现多种源类型。我想创建一个源从一个CSV文件中读取,二进制等。变量不够长,当存储csv :: DecodedRecords迭代器
我使用rust-csv
库解串CSV数据:
#[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
}
}
}
我似乎无法存储参考通过CSV读者由于寿命问题返回DecodedRecords
迭代器:
error: reader does not live long enough
我怎样才能存到解码记录迭代器的引用,我究竟做错了什么?
感谢您的明确解释! –