【发布时间】:2020-05-30 21:00:02
【问题描述】:
我对 Rust 的工作原理有点陌生,即使在尝试了几个月之后。我正在用 R2D2 (0.8.0) 和 Rocket (0.4.4) + Rocket_cors (0.5.2) 编写一个网络服务器
Rocket 允许您将它的结构提供给状态池,因此我不必在每次有人发送请求时都初始化我的 Postgres 服务器。代码如下:
struct ConnectionPool<M: r2d2::ManageConnection> {
val: r2d2::Pool<M>
}
#[post("/upload", format = "application/json", data = "<data>")]
fn upload(data: Data, state: rocket::State<ConnectionPool>) {
if data.peek_complete() {
println!("All of the data: {:?}", str::from_utf8(data.peek()).unwrap());
}
//data.stream_to_file(env::temp_dir().join("upload.txt"))
// .map(|n| n.to_string())
// .map_err(Debug)
}
显然,这只是代码的 sn-p,但 r2d2::ConnectionPool 需要一个类型标识符:如果我忽略该要求,则会出现此错误(在上面代码 sn-p 的第 4 行):
wrong number of type arguments: expected 1, found 0
expected 1 type argumentrustc(E0107)
main.rs(45, 44): expected 1 type argument
但是当我尽力通过更新代码来解决问题时:
fn upload(data: Data, state: rocket::State<ConnectionPool>) {
|
v
fn upload(data: Data, state: rocket::State<ConnectionPool<r2d2::ManageConnection>>) {
我收到此错误:
the size for values of type `(dyn r2d2::ManageConnection + 'static)` cannot be known at compilation time
doesn't have a size known at compile-time
help: the trait `std::marker::Sized` is not implemented for `(dyn r2d2::ManageConnection + 'static)`
【问题讨论】:
-
r2d2::ManageConnection是一个特征。您需要指定您正在使用的实际、具体的连接类型。您没有共享创建连接池的代码,因此我们无法告诉您该类型是什么。
标签: rust