Actix TCP客户端实现的编译问题

时间:2020-10-25 20:33:16

标签: rust actix-web rust-actix

我对Rust还是很陌生,并且愿意为Actix-web中的Orange Pi Zero提供一些Linux服务,该服务将充当其他网络设备的“网关”(一些愚蠢的中文网络中继,具有4个输入和4个输出(由AT命令通过TCP或UDP控制),并将异步轮询该设备,并通过WebSocket将其输出转换为不断刷新的网页。我想创建2个不同的actor,第一个应该是TCP客户端actor,它通过run_interval()轮询中继,使用适当的AT命令发出网络请求,读取响应并将消息发送到WebSocket actor,它将通过将状态输入到网页。 我成功实现了一个WebSocket,并尝试使用TcpStream,FramedWrite和LinesCodec实现另一个actor。 我的TCP actor看起来像这样:

struct TcpClientActor {
    framed: actix::io::FramedWrite<
        String,
        WriteHalf<TcpStream>,
        LinesCodec,
    >,
}

我还有很多其他特质实现,它们的编译没有任何问题,但是卡在结构本身上,编译器抱怨以下错误:

the trait `tokio::io::async_write::AsyncWrite` is not implemented for `tokio::io::split::WriteHalf<tokio::net::tcp::stream::TcpStream>`

我检查了Actix的源代码,发现AsyncWrite已经为WriteHalf和TcpStream实现了,但是无法正确编译它。

这是我的Cargo.toml的[dependencies]部分:

[dependencies]
# sysfs_gpio = "0.5"
actix = "0.10"
actix-codec = "0.3"
actix-web = "3"
actix-web-actors = "3"
actix-files = "0.3"
env_logger = "0.7"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
json = "0.12"
#mio = "0.7"
tokio = { version = "0.3.1", features = ["full", "tracing"] }
tokio-util = "0.3.1"

此外,我想用Supervisor包装第一个actor来处理断开连接和其他网络问题。请提供任何示例。

将感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可能遇到了一个令人困惑的依赖冲突的案例。

actix板条箱依赖于tokio 0.2.6。但是,您已将依赖项列为tokio 0.3.1.。 Cargo认为0.2.x和0.3.x不兼容,因此在您的项目中将包含两个版本的tokio。错误消息的原因是您使用的是WriteHalf的0.3版本,但actix FramedWrite正在执行实现AsyncWrite的0.2版本的操作。

解决方法可能是将您的tokio版本降级到0.2

另请参阅:Why is a trait not implemented for a type that clearly has it implemented?