我正在尝试使用PickleDB(内存中的键/值存储)编写基于Rocket / Juniper / Rust 的 GraphQL 服务器。
pickle db 在开始时创建/加载,并交给 Rocket 管理:
fn rocket() -> Rocket {
let pickle_path = var_os(String::from("PICKLE_PATH")).unwrap_or(OsString::from("pickle.db"));
let pickle_db_dump_policy = PickleDbDumpPolicy::PeriodicDump(Duration::from_secs(120));
let pickle_serialization_method = SerializationMethod::Bin;
let pickle_db: PickleDb = match Path::new(&pickle_path).exists() {
false => PickleDb::new(pickle_path, pickle_db_dump_policy, pickle_serialization_method),
true => PickleDb::load(pickle_path, pickle_db_dump_policy, pickle_serialization_method).unwrap(),
};
rocket::ignite()
.manage(Schema::new(Query, Mutation))
.manage(pickle_db)
.mount(
"/",
routes![graphiql, get_graphql_handler, post_graphql_handler],
)
}
我想从我的 Guard 中的 Rocket State中检索 PickleDb 实例:
pub struct Context {
pickle_db: PickleDb,
}
impl juniper::Context for Context {}
impl<'a, 'r> FromRequest<'a, 'r> for Context {
type Error = ();
fn from_request(_request: &'a Request<'r>) -> request::Outcome<Context, ()> {
let pickle_db = _request.guard::<State<PickleDb>>()?.inner();
Outcome::Success(Context { pickle_db })
}
}
这不起作用,因为国家只给了我一个参考:
26 | Outcome::Success(Context { pickle_db })
| ^^^^^^^^^ expected struct `pickledb::pickledb::PickleDb`, found `&pickledb::pickledb::PickleDb`
当我更改 Context 结构以包含引用时,我会遇到我还不熟悉的生命周期问题:
15 | pickle_db: &PickleDb,
| ^ expected named lifetime parameter
我尝试使用 'static 确实让 rust 非常不开心,我尝试使用 FromRequest 的请求生命周期 (?) 'r,但这也不起作用......
我怎样才能让它工作?由于我对生锈很陌生,这是做事的正确方法吗?