gpt4 book ai didi

rust - 发现结构特征错误: Wrong number of type arguments: Expected 1, 0

转载 作者:行者123 更新时间:2023-12-03 11:37:24 26 4
gpt4 key购买 nike

即使经过几个月的试验,Rust的工作原理我还是很陌生。我正在使用R2D2(0.8.0)和Rocket(0.4.4)+ Rocket_cors(0.5.2)编写Web服务器

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)
}

显然,这只是代码的一部分,但是r2d2::ConnectionPool需要一个类型标识符:如果我忽略了此要求,则会出现此错误(在上面的代码片段的第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是一个特征。无需重复特征名称,您需要将其传递给实现该特征的具体类型。由于您使用的是Postgres,因此您可能需要:

fn upload(data: Data, state: rocket::State<ConnectionPool<PostgresConnectionManager>>)

或者,如果要使代码数据库不可知,则可以使该方法在连接类型上通用,然后让调用者确定:
fn upload<M: r2d2::ManageConnection>(data: Data, state: rocket::State<ConnectionPool<M>>)

关于rust - 发现结构特征错误: Wrong number of type arguments: Expected 1, 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62108619/

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