テストを書く際に、リクエストに接続を挿入して、テストケース全体をトランザクションにラップすることができるようにしたいと思います。テストケースの要求)。ディーゼル接続をIronミドルウェアに注入する
私は、次のような、接続を挿入するために、私は私のテストケースにリンクすることができBeforeMiddleware
を使用してこれを実行しようとしました:
pub type DatabaseConnection = PooledConnection<ConnectionManager<PgConnection>>;
pub struct DatabaseOverride {
conn: DatabaseConnection,
}
impl BeforeMiddleware for DatabaseOverride {
fn before(&self, req: &mut Request) -> IronResult<()> {
req.extensions_mut().entry::<DatabaseOverride>().or_insert(self.conn);
Ok(())
}
}
しかし、私がやろうとしてコンパイルエラーに遭遇していますこの:
error: the trait bound `std::rc::Rc<diesel::pg::connection::raw::RawConnection>: std::marker::Sync` is not satisfied [E0277]
impl BeforeMiddleware for DatabaseOverride {
^~~~~~~~~~~~~~~~
help: run `rustc --explain E0277` to see a detailed explanation
note: `std::rc::Rc<diesel::pg::connection::raw::RawConnection>` cannot be shared between threads safely
note: required because it appears within the type `diesel::pg::PgConnection`
note: required because it appears within the type `r2d2::Conn<diesel::pg::PgConnection>`
note: required because it appears within the type `std::option::Option<r2d2::Conn<diesel::pg::PgConnection>>`
note: required because it appears within the type `r2d2::PooledConnection<r2d2_diesel::ConnectionManager<diesel::pg::PgConnection>>`
note: required because it appears within the type `utility::db::DatabaseOverride`
note: required by `iron::BeforeMiddleware`
error: the trait bound `std::cell::Cell<i32>: std::marker::Sync` is not satisfied [E0277]
impl BeforeMiddleware for DatabaseOverride {
^~~~~~~~~~~~~~~~
help: run `rustc --explain E0277` to see a detailed explanation
note: `std::cell::Cell<i32>` cannot be shared between threads safely
note: required because it appears within the type `diesel::pg::PgConnection`
note: required because it appears within the type `r2d2::Conn<diesel::pg::PgConnection>`
note: required because it appears within the type `std::option::Option<r2d2::Conn<diesel::pg::PgConnection>>`
note: required because it appears within the type `r2d2::PooledConnection<r2d2_diesel::ConnectionManager<diesel::pg::PgConnection>>`
note: required because it appears within the type `utility::db::DatabaseOverride`
note: required by `iron::BeforeMiddleware`
ディーゼルの接続でこれを回避する方法はありますか?私はpg
木箱を使ってGithubにいくつかの例を見つけましたが、私はディーゼルを使い続けたいと思います。
私は間違っているかもしれませんが(私は自分のRust環境atm近くではありません)、あなたは 'typemap'のような特性を実装する必要はありませんか? –
はい、私は実装しました(私は簡潔さのためにここに掲載しませんでした)。ここでの問題は、 'PgConnection'が' Sync'を実装していないため、 'BeforeMiddleware'を' diesel :: pg :: PgConnection'で初期化できないことです。私は誰かがこの制限の回避策を知っていることを期待していました。 – NeuroXc
自分で「ディーゼル」を使っていないのは分かりません。しかし、 'Cell'型のいずれか、あるいは' Rc'のような非同期型スマートポインタを使用していると、自動的に 'Sync'を実装する能力に失敗します。 –