我試圖寫一個鐵的插件中間件,但我試圖定義一個typemap
鍵時遇到了一個問題:特質類型和壽命問題
與簡單類型的小例子,工作沒有問題:
pub struct Database;
impl Key for Database {
type Value = isize;
}
但只要一生參與,我不能編譯庫:
pub struct Database<'a> {
pool: &'a Arc<Pool<PostgresConnectionManager>>
}
impl<'a> Key for Database<'a> {
type Value = PooledConnection<'a, PostgresConnectionManager>;
}
這裏發生了什麼?我得到的錯誤:
src/lib.rs:33:1: 35:2 note: first, the lifetime cannot outlive the lifetime 'a as defined on the impl at 33:0...
src/lib.rs:33 impl<'a> Key for Database<'a> {
src/lib.rs:34 type Value = PooledConnection<'a, PostgresConnectionManager>;
src/lib.rs:35 }
src/lib.rs:33:1: 35:2 note: ...so that trait type parameters matches those specified on the impl (expected `typemap::Key`, found `typemap::Key`)
src/lib.rs:33 impl<'a> Key for Database<'a> {
src/lib.rs:34 type Value = PooledConnection<'a, PostgresConnectionManager>;
src/lib.rs:35 }
note: but, the lifetime must be valid for the static lifetime...
src/lib.rs:33:1: 35:2 note: ...so that the type `r2d2::PooledConnection<'_, r2d2_postgres::PostgresConnectionManager>` will meet its required lifetime bounds
src/lib.rs:33 impl<'a> Key for Database<'a> {
src/lib.rs:34 type Value = PooledConnection<'a, PostgresConnectionManager>;
src/lib.rs:35 }
但是,這沒有任何意義,我 - 的PooledConnection
不能活得比經理和Arc<Pool<...Manager>>
是因爲一生來確保這一點。我在這裏錯過了什麼?