【问题标题】:Is there a way to create a Box from a Vec<u8> that implements the Read trait?有没有办法从实现 Read 特征的 Vec<u8> 创建一个 Box ?
【发布时间】:2019-09-12 20:30:40
【问题描述】:

我正在编写一段代码,使用 lopdf crate 在 Vec 上写入 PDF。然后我想通过 IPP crate 打印 PDF,但它需要一个我无法创建的 Box

我尝试了很多事情,但我总是遇到所有权问题。 我尝试的另一件事是从原始指针创建 Box,但它确实使程序崩溃。

let mut doc = Document::load("file.pdf").unwrap();
let mut doc_bytes = Vec::new();
//redacted
doc.save_to(&mut doc_bytes);
let doc_slice = doc_bytes.as_slice();
let buffer = std::io::BufReader::new(doc_slice.to_vec().as_slice());
let doc_box = Box::from(buffer);
let client = ipp::IppClient::new("printer URL");
let print_job = ipp::operation::PrintJob::new(doc_box, &"username", None)
client.send(print_job).unwrap();

它应该在打印机上打印文件。它不会编译这样说

temporary value dropped while borrowed

creates a temporary which is freed while still in use rustc(E0716)
main.rs(6, 70): creates a temporary which is freed while still in use
main.rs(6, 100): temporary value is freed at the end of this statement
main.rs(9, 79): cast requires that borrow lasts for `'static`

doc_slice.to_vec().

基本上,我正在尝试做这样的事情 (https://github.com/dremon/ipp.rs/blob/master/ipp-client/examples/print-job.rs),但没有从文件中读取。

最小、完整和可验证的示例

Cargo.toml

[package]
name = "example"
version = "0.1.0"
authors = ["you <you@example.com>"]
edition = "2018"

[dependencies]
ipp = "0.2.1"

main.rs

fn main(){
  let mut doc_bytes = Vec::<u8>::new();
  doc_bytes.set_len(720000);
  let client = ipp::IppClient::new("http://printer_url");
  let print_job = ipp::operation::PrintJob::new(doc_bytes.into_boxed_slice(), &"user", None);
  client.send(print_job).unwrap();
}

使用此代码我得到两个错误:

   Compiling example v0.1.0 (/path)
error[E0277]: the trait bound `[u8]: std::io::Read` is not satisfied
 --> src/main.rs:5:49
  |
5 |   let print_job = ipp::operation::PrintJob::new(doc_bytes.into_boxed_slice(), &"username", None);
  |                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::io::Read` is not implemented for `[u8]`
  |
  = help: the following implementations were found:
            <&'a [u8] as std::io::Read>
  = note: required for the cast to the object type `dyn std::io::Read`

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/main.rs:5:49
  |
5 |   let print_job = ipp::operation::PrintJob::new(doc_bytes.into_boxed_slice(), &"username", None);
  |                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `[u8]`
  = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
  = note: required for the cast to the object type `dyn std::io::Read`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
error: Could not compile `pettorine`.

To learn more, run the command again with --verbose.

我注意到我收到了一个新错误!

提前感谢大家的支持

【问题讨论】:

  • 并且 Read 已经实现 Impl&lt;'_&gt; Read for &amp;'_ [u8]
  • @turbulencetoo 编译器说the trait bound `[u8]: std::io::Read` is not satisfied. the trait `std::io::Read` is not implemented for `[u8]`
  • 但它是为 &[u8] 实现的,并且 A Box 应该引用一个 &[u8]
  • 您能否提供一个minimal reproducible example 以及相应的错误消息? Rust tag info 包含有关如何创建它的提示。我猜您的问题与您要解决的实际问题无关。

标签: rust ownership


【解决方案1】:

好的,找到答案了 (https://github.com/dremon/ipp.rs/issues/6)

let mut doc = Document::load("pettorina.pdf").unwrap();
let mut doc_bytes = Vec::new();
let mut num = " ".repeat(((4.0 -(pettorina.numero as f32).log10().floor())/2.0) as usize);
num.push_str(&pettorina.numero.to_string());
doc.version = "1.4".to_string();
doc.replace_text(1, "num", &num);
doc.replace_text(1, "NOME", &pettorina.nome.to_ascii_uppercase());
doc.save_to(&mut doc_bytes);
let client = ipp::IppClient::new("printer_url");
let print_job = ipp::operation::PrintJob::new(Box::new(std::io::Cursor::new(doc_bytes)), &"pettorine", None);
client.send(print_job).unwrap();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    相关资源
    最近更新 更多