【发布时间】:2015-05-13 15:41:52
【问题描述】:
我正在尝试编写 Iron 插件中间件,但在尝试定义 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>> 被赋予了生命周期来确保这一点。我在这里错过了什么?
【问题讨论】: