- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想创建一个简单的 websocket 服务器。我想处理传入的消息并发送响应,但出现错误:
error: captured variable cannot escape `FnMut` closure body
--> src\main.rs:32:27
|
32 | incoming.for_each(|m| async {
| _________________________-_^
| | |
| | inferred to be a `FnMut` closure
33 | | match m {
34 | | // Error here...
35 | | Ok(message) => do_something(message, db, &mut outgoing).await,
36 | | Err(e) => panic!(e)
37 | | }
38 | | }).await;
| |_____^ returns a reference to a captured variable which escapes the closure body
|
= note: `FnMut` closures only have access to their captured variables while they are executing...
= note: ...therefore, they cannot allow references to captured variables to escape
这在 Stack Overflow 上有一些点击,但我在代码中没有看到变量正在转义的任何地方。异步块不会同时运行,所以我看不出任何问题。此外,我觉得我在做一些非常简单的事情:我得到了一个类型,它允许我将数据发送回客户端,但是当在 async 块中使用对它的引用时,它会产生编译错误。该错误仅在我在异步代码中使用
outgoing
或
db
变量时发生。
handle_connection
函数中):
use tokio::net::{TcpListener, TcpStream};
use std::net::SocketAddr;
use std::sync::Arc;
use futures::{StreamExt, SinkExt};
use tungstenite::Message;
use tokio_tungstenite::WebSocketStream;
struct DatabaseConnection;
#[tokio::main]
async fn main() -> Result<(), ()> {
listen("127.0.0.1:3012", Arc::new(DatabaseConnection)).await
}
async fn listen(address: &str, db: Arc<DatabaseConnection>) -> Result<(), ()> {
let try_socket = TcpListener::bind(address).await;
let mut listener = try_socket.expect("Failed to bind on address");
while let Ok((stream, addr)) = listener.accept().await {
tokio::spawn(handle_connection(stream, addr, db.clone()));
}
Ok(())
}
async fn handle_connection(raw_stream: TcpStream, addr: SocketAddr, db: Arc<DatabaseConnection>) {
let db = &*db;
let ws_stream = tokio_tungstenite::accept_async(raw_stream).await.unwrap();
let (mut outgoing, incoming) = ws_stream.split();
// Adding 'move' does also not work
incoming.for_each(|m| async {
match m {
// Error here...
Ok(message) => do_something(message, db, &mut outgoing).await,
Err(e) => panic!(e)
}
}).await;
}
async fn do_something(message: Message, db: &DatabaseConnection, outgoing: &mut futures_util::stream::SplitSink<WebSocketStream<TcpStream>, Message>) {
// Do something...
// Send some message
let _ = outgoing.send(Message::Text("yay".to_string())).await;
}
Cargo.toml
[dependencies]
futures = "0.3.*"
futures-channel = "0.3.*"
futures-util = "0.3.*"
tokio = { version = "0.2.*", features = [ "full" ] }
tokio-tungstenite = "0.10.*"
tungstenite = "0.10.*"
使用
async move
时,出现以下错误:
incoming.for_each(|m| async move {
let x = &mut outgoing;
let b = db;
}).await;
错误
error[E0507]: cannot move out of `outgoing`, a captured variable in an `FnMut` closure
--> src\main.rs:33:38
|
31 | let (mut outgoing, incoming) = ws_stream.split();
| ------------ captured outer variable
32 |
33 | incoming.for_each(|m| async move {
| ______________________________________^
34 | | let x = &mut outgoing;
| | --------
| | |
| | move occurs because `outgoing` has type `futures_util::stream::stream::split::SplitSink<tokio_tungstenite::WebSocketStream<tokio::net::tcp::stream::TcpStream>, tungstenite::protocol::message::Message>`, which does not implement the `Copy` trait
| | move occurs due to use in generator
35 | | let b = db;
36 | | }).await;
| |_____^ move out of `outgoing` occurs here
最佳答案
FnMut
是一个匿名结构体,因为 FnMut
捕获了 &mut outgoing
,它成为这个匿名结构体内部的一个字段,这个字段将在每次调用 FnMut
时使用,它可以被多次调用。如果您以某种方式丢失了它(通过返回或移动到另一个范围等...),您的程序将无法使用该字段进行进一步调用,因为安全 Rust 编译器不允许您这样做(对于您的两种情况)。
在您的情况下,我们可以将其用作每次调用的参数,而不是捕获 &mut outgoing
,这样我们将保留 outgoing
的所有权。您可以通过使用 futures-rs 中的 fold 来做到这一点:
incoming
.fold(outgoing, |mut outgoing, m| async move {
match m {
// Error here...
Ok(message) => do_something(message, db, &mut outgoing).await,
Err(e) => panic!(e),
}
outgoing
})
.await;
这可能看起来有点棘手,但它可以完成工作,我们使用常量累加器(
outgoing
),它将用作
FnMut
的参数。
关于rust - future 生成器关闭时出错 : Captured variable cannot escape `FnMut` closure body,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62557219/
我一直在阅读 Captures这一段引起了我的兴趣: Inside a Signature, a Capture may be created by prefixing a sigilless par
我在 Java 中使用这个正则表达式: ^(Mon(?:.?|day)?)(?:[\.,])?$ (可以测试 here ) 我想捕获日期,后跟可选的 . 或 ,。如果是星期一,我想捕获 Monday
我正在 try catch 功能键 F1 到 F12 和 4 个箭头键以及主页、插入、删除、结束、向上翻页和向下翻页键。如何???? private void Form1_KeyPress(objec
没有capture="camera" input type="file" 的属性标签 in official w3.org documentation . 讽刺的是,我发现了这么多地方 capture
摘自Huon Wilson的Finding Closure in Rust: Capturing entirely by value is also strictly more general tha
所以我想这样做: public interface IFieldObject { public Comparable get(); } public interface IFieldCondi
我希望使用正则表达式将单词分成组(vowels, not_vowels, more_vowels),使用标记来确保每个单词以元音开头和结尾。 import re MARKER = "~" VOWELS
我在浏览 StackOverflow 时发现了 Szimek/Signature_Pad 以使用 Javascript 捕获电子/数字签名。 我研究过,但我仍然对如何将 DATA URI 捕获到变量中
我正在尝试关注 this example使用带有 remove_if 的 lambda。这是我的尝试: int flagId = _ChildToRemove->getId(); auto new_e
我无法捕获 在屏幕捕获区域内。 我想要一个定义的部分,其中包含要捕获的图像和内容。我们怎样才能做到这一点?帮助! 访问:https://stackblitz.com/edit/ngx-capture-
从 Perl 脚本调用外部程序时,Capture::Tiny 是否避免了使用 system() 时需要的磁盘 io?使用任何一种时,我都能获得基本相同的性能。一位同事正在使用我的代码并告诉我它正在敲打
作为数值方法研究的一部分,我正在编写一个函数来解决流值问题。这是该程序的“核心”,但它出现了一些奇怪的错误,这很奇怪,因为我在其他程序中使用了相同的代码段而没有出现任何错误。 void solve_
vector vec; //a auto foo = [&vec](){ //do something }; //b auto foo = [&v = vec](){ //do som
我正在使用 PyDev 对我的 Python 应用程序进行开发和单元测试。至于单元测试,除了没有内容被记录到日志框架之外,一切都很好。 PyDev 的“捕获的输出”没有捕获记录器。 我已经将记录的所有
你能帮我解决这个编译器错误吗? template static void ComputeGenericDropCount(function func) { T::ForEach([](T *w
第一次做泛型,我有点困惑。 我有以下内容: public interface GenericDao { /** * Retrieve an object that was previ
我正在尝试提取此代码中 dir_entry.path() 的值并想将其复制到 compFileName 中。问题是我一直收到错误“compFileName cannot be implicitly c
我正在使用在网上找到的 WebCam_Capture 代码通过 C# 访问网络摄像头。在一台只有一个视频源的计算机上,它就像一个魅力! (程序在启动时启动,找到网络摄像头并正常工作)。 虽然在一台有很
下面的代码 void CMainWindow::someMethod(const CLocationsCollection& parentItem) { auto f = [this, par
所以我打开了一个 youtube 页面,我可以在那里观看视频。 但是这个视频被用户下架了。我打开的页面仍然有视频,如果你再次访问(刷新)新页面没有。 由于我在浏览器选项卡 (chrome) 中加载了视
我是一名优秀的程序员,十分优秀!