gpt4 book ai didi

rust - 有没有办法从实现 Read 特征的 Vec 创建一个 Box?

转载 作者:行者123 更新时间:2023-11-29 08:36:12 27 4
gpt4 key购买 nike

我正在编写一段代码,使用 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.

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

在此先感谢您的支持

最佳答案

好的,找到答案 ( 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();

关于rust - 有没有办法从实现 Read 特征的 Vec<u8> 创建一个 Box?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55814869/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com