- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用seanmonstar/warp构建我的休息服务,并遇到了与生命周期相关的问题。这是我的应用程序启动代码的样子:
struct MyModelStruct {
//...
}
struct Database {
//...
}
impl Database {
fn connect(/* ommited */) -> Database {
//...
}
}
fn start_app(db: &Database) -> () {
let route = warp::post()
.and(warp::path!("action" / u32))
.and(warp::body::json::<MyModelSctruct>())
.map(|_, _| {
//use db
})
warp::serve(route).run(/* address */);
}
我得到了错误:
error[E0621]: explicit lifetime required in the type of `db`
--> src/application.rs:69:5
|
46 | fn start_app(db: &Database) -> (){
| -------- help: add explicit lifetime `'static` to the type of `db`: `&'static db::Database`
...
69 | warp::serve(route);
| ^^^^^^^^^^^ lifetime `'static` required
这是因为
warp::serve
函数定义为
/// Create a `Server` with the provided `Filter`.
pub fn serve<F>(filter: F) -> Server<F>
where
F: Filter + Clone + Send + Sync + 'static,
F::Extract: Reply,
F::Error: IsReject,
{
Server {
pipeline: false,
filter,
}
}
因此,明确要求
'static
生存期。问题是它被用作
let db = Database::connect(...);
start_app(&db);
因此,db的生存期不是
static
。有办法解决此问题吗?
最佳答案
'static
生存期意味着在删除函数本身之前,保证该函数不会引用任何可能删除的数据。因为您当前正在传递&Database
,所以您的函数不能是'static
(除非您以某种方式传递了&'static Database
,这将需要显式创建内存泄漏)。
解决此问题的唯一方法是让您传递Database
或Arc<Database>
或允许访问的内容,以便该功能对Database
的强大引用,并可以确保只要该功能存在,DB就将继续存在。
关于rust - 如果外部包装箱明确要求静态生命周期,该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63106684/
我正在开发一个使用多个 turtle 的滚动游戏。玩家 turtle 根据按键命令在 Y 轴上移动。当危害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。我尝试定义一个名为 colliding(
我不明白为什么他们不接受这个作为解决方案,他们说这是一个错误的答案:- #include int main(void) { int val=0; printf("Input:- \n
我正在使用基于表单的身份验证。 我有一个注销链接,如下所示: 以及对应的注销方法: public String logout() { FacesContext.getCurren
在 IIS7 应用程序池中有一个设置 Idle-time out 默认是 20 分钟,其中说: Amount of time(in minutes) a worker process will rem
我是一名优秀的程序员,十分优秀!