作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在研究一种实现Stream
特征的类型。它基本上需要另外两个流,从它们中获取next()
值,然后根据某些条件返回一个next()
值本身。我想到了这样的代码设置:
impl Stream for HeartbeatStream {
type Item = char;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
// ...
// Dealing with heartbeat and no heartbeat
match &mut self.next_heartbeat {
Some(hb) => {
Pin::new(hb).poll(cx);
tokio::spawn(async move {
tokio::select! {
Some(char) = stream1.next(), if self.cooldown.map_or(true, |cd| since_last_signal > cd) => {
self.reset_last_signal_next_heartbeat();
return Poll::Ready(Some(char))
},
Some(char) = stream2.next(), if self.cooldown.map_or(true, |cd| since_last_signal > cd) => {
self.reset_last_signal_next_heartbeat();
return Poll::Ready(Some(char))
},
_ = hb => {
self.reset_last_signal_next_heartbeat();
return Poll::Ready(Some(self.char))
},
else => Poll::Ready(None),
}
});
},
None => {
...
}
}
Poll::Pending
}
}
Full code is shown here。
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
--> src/heartbeat.rs:59:14
|
59 | match &mut self.next_heartbeat {
| ^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 52:2...
--> src/heartbeat.rs:52:2
|
52 | fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
| _____^
53 | | let mut stream1 = self.streams[0];
54 | | let mut stream2 = self.streams[1];
55 | | let since_last_signal = Instant::now().saturating_duration_since(
... |
97 | | Poll::Pending
98 | | }
| |_____^
note: ...so that the reference type `&mut std::pin::Pin<&mut heartbeat::HeartbeatStream>` does not outlive the data it points at
--> src/heartbeat.rs:59:14
|
59 | match &mut self.next_heartbeat {
| ^^^^
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `impl std::future::Future` will meet its required lifetime bounds
--> src/heartbeat.rs:62:5
|
62 | tokio::spawn(async move {
| ^^^^^^^^^^^^
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> src/heartbeat.rs:62:29
|
62 | tokio::spawn(async move {
| _________________________________________^
63 | | tokio::select! {
64 | | Some(char) = stream1.next(), if self.cooldown.map_or(true, |cd| since_last_signal > cd) => {
65 | | self.reset_last_signal_next_heartbeat();
... |
77 | | }
78 | | });
| |_________________^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 52:2...
--> src/heartbeat.rs:52:2
|
52 | fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
| _____^
53 | | let mut stream1 = self.streams[0];
54 | | let mut stream2 = self.streams[1];
55 | | let since_last_signal = Instant::now().saturating_duration_since(
... |
97 | | Poll::Pending
98 | | }
| |_____^
note: ...so that the types are compatible
--> src/heartbeat.rs:62:29
|
62 | tokio::spawn(async move {
| _________________________________________^
63 | | tokio::select! {
64 | | Some(char) = stream1.next(), if self.cooldown.map_or(true, |cd| since_last_signal > cd) => {
65 | | self.reset_last_signal_next_heartbeat();
... |
77 | | }
78 | | });
| |_________________^
= note: expected `std::pin::Pin<&mut heartbeat::HeartbeatStream>`
found `std::pin::Pin<&mut heartbeat::HeartbeatStream>`
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `impl std::future::Future` will meet its required lifetime bounds
--> src/heartbeat.rs:62:5
|
62 | tokio::spawn(async move {
| ^^^^^^^^^^^^
Full error code is shown here。
tokio::spawn()
外部以归入异步代码内部来解决。但是在这种情况下,它提示
self
对象。
最佳答案
Rust编译器在删除tokio::spawn()
之前不知道self
是否将加入。即使tokio::spawn()
返回了可以传递给JoinHandle
且从未连接的std::mem::forget()
,也使得闭包可以访问已删除的self
引用(这是不安全的)。这就是为什么编译器强制执行'static
生存期的原因。通常,“Rust的安全保证不包括析构函数将始终运行的保证”(source)。我建议您直接调用next_pool()
而不是next()
。
关于rust - 调用tokio::spawn时如何设置 `self`的适当生存期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64509968/
我正在使用身份服务器 3 保护 Web 应用程序。我的应用程序分为 2 个 oidc 客户端,一个 ASP.Net MVC 客户端和一个使用 oidc-client javascript 库的 jav
我正在使用 Silex 构建一个小型后台项目,我正在使用 PdoSessionHandler 将 session 存储在数据库中。我已经成功地将 session 存储在数据库中,但似乎将 cookie
我是一名优秀的程序员,十分优秀!