【问题标题】:Rocket's State errors with "Attempted to retrieve unmanaged state" when using an explicit lifetime?使用显式生命周期时,Rocket 的状态错误“尝试检索非托管状态”?
【发布时间】:2019-09-16 23:40:55
【问题描述】:

当使用 Rocket 的 State 并省略生命周期时,可以正常处理对路由的请求:

#[post("/foo")]
pub fn foo_handler(db: State<Db>) {
    // ...
}

但是,如果提供了明确的生命周期,那么使用 Attempted to retrieve unmanaged state! 的请求会出现 Rocket 错误:

#[post("/foo")]
pub fn foo_handler<'a>(db: State<&'a Db>) {
    // ...
}

编译器没有在此处提取某些内容,或者 Rocket 避免了安全检查,因为这样编译正常,没有任何错误或警告。有什么想法吗?

【问题讨论】:

  • 我认为这些签名不同。 db: State&lt;&amp;'static Db&gt; 是做什么的?
  • 不,仍然是同样的错误并且编译时没有警告。

标签: rust rust-rocket


【解决方案1】:

这似乎是达到所需结果的方法:

#[post("/foo")]
pub fn foo_handler<'a>(db: State<'a, Db>) {
  // ...
}

Rocket 的 State 文档中提供了一个示例。不过,我希望上述实现会引发错误,因为它是有效的语法。

【讨论】:

    【解决方案2】:

    我发现这个错误是由于未能调用 unwrap() 对我正在初始化以在 State 中使用的值。

    let index = load().unwrap(); // <-- without unwrap, compiled but failed on request
    rocket::ignite()
      .manage(index) // normal mount and so on here
    ... etc ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多