【发布时间】:2018-10-26 07:25:47
【问题描述】:
我正在尝试提取 .tar.bz 文件(实际上是 .tar.whatever),并且还能够获得 xx% 的进度报告。到目前为止,我有这个:
pub fn extract_file_with_progress<P: AsRef<Path>>(&self, path: P) -> Result<()> {
let path = path.as_ref();
let size = fs::metadata(path)?;
let mut f = File::open(path)?;
let decoder = BzDecoder::new(&f);
let mut archive = Archive::new(decoder);
for entry in archive.entries()? {
entry?.unpack_in(".")?;
let pos = f.seek(SeekFrom::Current(0))?;
}
Ok(())
}
这个想法是使用pos/size 来获取百分比,但是编译上面的函数会得到错误cannot borrow f as mutable because it is also borrowed as immutable。
我理解错误的含义,但我并没有真正将f 用作可变的;我只使用seek函数来获取当前位置。
有没有办法解决这个问题,要么强制编译器忽略可变借用,要么以某种不可变的方式获取位置?
【问题讨论】:
-
seek()采用&mut self,所以这是可变借用发生的地方。查看 crate 文档,您不能使用decoder.total_in()代替吗? -
请查看如何创建minimal reproducible example,然后查看edit 您的问题以包含它。我们无法分辨代码中存在哪些 crate、类型、特征、字段等。具体而言,我们无法分辨
BzDecoder和Archive来自哪里,以及其他一些特征。尝试在Rust Playground 上重现您的错误,或者您可以在全新的 Cargo 项目中重现它。还有Rust-specific MCVE tips。