使用refs实现迭代器时的寿命推断问题

时间:2018-09-01 22:41:03

标签: iterator rust lifetime

我正在为包含ref的结构实现一个简单的Iterator:

extern crate zip;
extern crate quick_xml;
extern crate chrono;

use std::io::{Seek, Write, Read, Error};
use std::fs::File;
use xlsx_read::zip::read::ZipFile;
use xlsx_read::zip::result::ZipResult;
use xlsx_read::zip::ZipArchive;
use xlsx_read::zip::write::{FileOptions, ZipWriter};
use xlsx_read::quick_xml::Reader as XmlReader;
use xlsx_read::quick_xml::events::Event;
use std::io::BufReader;
use xlsx_read::chrono::prelude::*;

pub struct XlsxFile<'a> {
    path: &'a str,
    archive: ZipArchive<File>,
    sheet_count: usize,
    curr: usize,
}

impl<'a> XlsxFile<'a> {
    pub fn from(path: &'a str) -> Result<XlsxFile, Error> {
        let file = File::open(path)?;
        let archive = ZipArchive::new(file)?;
        let sheet_count = archive.len();

        Ok(XlsxFile {
            path: path,
            archive: archive,
            sheet_count,
            curr: 0,
        })
    }
}

pub struct XlsxSheet<'a> {
    pub name: &'a str,
    pub index: usize,
}

impl<'a> Iterator for XlsxFile<'a> {
    type Item = XlsxSheet<'a>;

    fn next(&mut self) -> Option<XlsxSheet<'a>> {
        loop {
            if self.sheet_count > 0 &&
                self.sheet_count > self.curr {
                let zip_file = self.archive.by_index(self.curr).unwrap();
                let file_name = zip_file.name();
                if file_name.contains("xl/worksheets/sheet") {
                    let sheet = XlsxSheet {
                        name: file_name, // works fine if String::from(file_name) is used
                        index: self.curr,
                    };
                    self.curr += 1;
                    return Some(sheet);
                }
                self.curr += 1;
                continue;
            } else {
                break;
            }
        }
        return None;
    }
}

static XLSX_FILE: &'static str = "<location_to_xlsx_file>";

fn main() {
    let mut file = xlsx_read::XlsxFile::from(XLSX_FILE).unwrap();

    file.for_each(|s| println!("idx: {:?}", s.name));
}

但是出现以下错误:

error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
  --> src/xlsx_read.rs:50:45
   |
50 |                 let zip_file = self.archive.by_index(self.curr).unwrap();
   |                                             ^^^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 46:5...
  --> src/xlsx_read.rs:46:5
   |
46 | /     fn next(&mut self) -> Option<XlsxSheet<'a>> {
47 | |         loop {
48 | |             if self.sheet_count > 0 &&
49 | |                 self.sheet_count > self.curr {
...  |
66 | |         return None;
67 | |     }
   | |_____^
note: ...so that reference does not outlive borrowed content
  --> src/xlsx_read.rs:50:32
   |
50 |                 let zip_file = self.archive.by_index(self.curr).unwrap();
   |                                ^^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 43:1...
  --> src/xlsx_read.rs:43:1
   |
43 | impl<'a> Iterator for XlsxFile<'a> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: ...so that the expression is assignable:
           expected std::option::Option<xlsx_read::XlsxSheet<'a>>
              found std::option::Option<xlsx_read::XlsxSheet<'_>>

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.

我的问题是,如何在这里告诉Rust编译器使用适当的生存期?即使我已经使用生命周期修饰符定义了XlsxSheet <'a>,并且想将名称绑定到&'a str,但是以某种方式,这并不能转换为有效的Rust代码。

1 个答案:

答案 0 :(得分:1)

简便解决方案:可以使用String而不是&'a str来解决此问题。

说明

我不知道by_index的定义,这似乎对这个问题至关重要。以下推理仅是猜测,并不可靠。仅供参考。

  1. self.archive借用了self(在整个范围内都是有效的,假设生存期被命名为'me),并且生存期为'me
  2. 因此,by_index的返回值具有生存期'me
  3. 糟糕,XlsxSheet<'me>XlsxSheet<'a>不兼容(这是预期的)!

我们想要的是XlsxSheet<'me>XlsxSheet<'a>的子类型,如果'me是协变的,这反过来意味着'aXlsxSheet的子类型。因此,您可以明确声明它们

fn next(&mut self) -> Option<XlsxSheet<'a>> where Self: 'a
// or
impl<'a> Iterator for XlsxFile<'a> + 'a