2017-07-22 14 views
1

r2d2 postgres crateを使用して接続プールを作成しようとしています。このインスタンスをdbConnの別のハンドラに渡してコンパイル時に不一致エラーが発生するようにします。 PostgresConnectionManagerがManageConnection特性を実装しているので、この不一致の原因を突き止めることができません。generic構造体を返すときに型が一致しません

ありがとうございます。

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://[email protected]: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`. 
+0

let d = DBPoll::<SqliteConnectionManager>::new(); 

のでrustcレポート構文エラー、この問題を解決するために、あなたは 正確な型を指定する必要があります - エラーメッセージを検索するだけで3つの重複が見つかりました。質問する前に**あなたの質問を**検索してください。 – Shepmaster

答えて

1

問題が2から1つのミスマッチということです:1

fn new()-> DBPool<M>/*1*/ { 
let manager = PostgresConnectionManager::new 
let p = r2d2::Pool::new(config, manager).unwrap() ; 
DBPool { pool: p}/*2*/ 
} 

あなたは私が ManageConnectionを実装する任意のDBPool<M>どこM任意の型を返す伝え、しかし2であなたは私の心が変わると伝えます私は具体的に戻るたとえば、このような署名のため= 、 はあなたが書くことができ:これは、何度も頼まれてい

fn new() -> DBPool<PostgresConnectionManager> { 
+0

**重複して回答しないでください**。代わりに、回答する前に検索し、そのようにマークします。 – Shepmaster