通过闭包时,不满足特征绑定 `std::io::Write + 'static: std::marker::Sized`

the trait bound `std::io::Write + 'static: std::marker::Sized` is not satisfied, when passing closure

我试图摆脱我程序中的一些代码重复,我决定使用一个函数,该函数将采用 Fn() -> Result<io::Write> 类型的闭包,当我请求时,它将为我提供输出流它。

所以这是这个函数:

fn dowload_body<F>(/* some params ommited */ write_supplier: F) -> Result<()>
  where F: Fn() -> Result<Write> {
  if ... {
    let mut destination = try!(write_supplier());
    // use this stream
  }
}

我想像这样使用它:

let destination_path = Path::new("some path");
let result = Self::dowload_body(|| try_str!(OpenOptions::new().append(true).open(destination_path)));

我收到以下错误:

src/http.rs:105:3: 121:4 error: the trait bound `std::io::Write + 'static: std::marker::Sized` is not satisfied [E0277]
src/http.rs:105   fn dowload_body<F>(write_supplier: F) -> Result<()>
                  ^

我是生锈新手,没能找到解决方案。

您不能将未调整大小的变量(特征)直接放入结果中。也许你是这个意思?

fn dowload_body<F, W: Write>(write_supplier: F) -> Result<()>
//                 ^
  where F: Fn() -> Result<W> 
//                        ^ create a template for the trait.
{