gpt4 book ai didi

rust - 返回通用结构时类型不匹配

转载 作者:行者123 更新时间:2023-11-29 08:18:43 25 4
gpt4 key购买 nike

<分区>

我正在尝试使用 r2d2 postgres crate 创建连接池,并将其保存在 struct DBPool 中,以便我可以将此实例传递给 dbConn 的不同处理程序,但在编译期间出现不匹配错误。无法找出这种不匹配的原因,因为 PostgresConnectionManager 实现了 ManageConnection 特征,所以这里缺少什么。

提前致谢。

extern crate r2d2;
extern crate r2d2_postgres;

use std::thread;
use r2d2:: {Pool, ManageConnection};
use r2d2_postgres::{TlsMode, PostgresConnectionManager};

fn main() {

let pool = DBPool::<PostgresConnectionManager>::new();
println!("{:?}", pool.pool);
}

struct DBPool <M: ManageConnection>{
pool: Pool<M>
}

impl<M: ManageConnection> DBPool<M> {
fn new()-> DBPool<M> {
let config = r2d2::Config::default();
let manager = PostgresConnectionManager::new("postgresql://root@localhost:26257/db?sslmode=disable", TlsMode::None).unwrap();
let p = r2d2::Pool::new(config, manager).unwrap() ;

println!("Pool p: {:?}", p);
DBPool { pool: p}
}
}

编译错误:

 dbcon git:(master) ✗ cargo run
Compiling dbcon v0.1.0 (file:///Users/arvindbir/projects/rust/dbcon)
error[E0308]: mismatched types
--> src/main.rs:42:9
|
42 | DBPool { pool: p}
| ^^^^^^^^^^^^^^^^^ expected type parameter, found struct `r2d2_postgres::PostgresConnectionManager`
|
= note: expected type `DBPool<M>`
found type `DBPool<r2d2_postgres::PostgresConnectionManager>`

error: aborting due to previous error

error: Could not compile `dbcon`.

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