【发布时间】:2019-02-28 08:24:57
【问题描述】:
我有一个客户处理 Future 做一些事情。是否可以使用impl Future<Item = (), Error = io::Error> 作为返回类型并进行更好的错误处理?
pub fn handle_client(client: Client) -> impl Future<Item = (), Error = io::Error> {
let magic = client.header.magic;
let stream_client = TcpStream::connect(&client.addr).and_then(|stream| {
let addr: Vec<u8> = serialize_addr(stream.local_addr()?, magic)?;
write_all(stream, addr).then(|result| {
// some code
Ok(())
})
});
stream_client
}
我无法在所有嵌套的闭包/期货中保留 io::Error 类型。编译器抛出错误
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> src/client.rs:134:29
|
134 | let addr: Vec<u8> = serialize_addr(stream.local_addr()?, magic)?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `futures::future::then::Then<tokio_io::io::write_all::WriteAll<tokio_tcp::stream::TcpStream, std::vec::Vec<u8>>, std::result::Result<(), std::io::Error>, [closure@src/client.rs:135:38: 138:10]>`
|
= help: the trait `std::ops::Try` is not implemented for `futures::future::then::Then<tokio_io::io::write_all::WriteAll<tokio_tcp::stream::TcpStream, std::vec::Vec<u8>>, std::result::Result<(), std::io::Error>, [closure@src/client.rs:135:38: 138:10]>`
= note: required by `std::ops::Try::from_error`
我进行了链接 map/and_then 错误处理,但问题是我不知道如何在 final .then 闭包中获取 TcpStream。我发现TcpStream 的唯一地方是在WriteAll 结构内,但它是私有的。另外,write_all 消费流
use futures::Future;
use std::{io, net::SocketAddr};
use tokio::{
io::{write_all, AsyncRead, AsyncWrite},
net::TcpStream,
};
type Error = Box<dyn std::error::Error>;
fn serialize_addr(addr: SocketAddr) -> Result<Vec<u8>, Error> {
Ok(vec![])
}
fn handle_client(addr: &SocketAddr) -> impl Future<Item = (), Error = Error> {
TcpStream::connect(addr)
.map_err(Into::into)
.and_then(|stream| stream.local_addr().map(|stream_addr| (stream, stream_addr)))
.map_err(Into::into)
.and_then(|(stream, stream_addr)| serialize_addr(stream_addr).map(|info| (stream, info)))
.map(|(stream, info)| write_all(stream, info))
.then(|result| {
let result = result.unwrap();
let stream = match result.state {
Writing { a } => a,
_ => panic!("cannot get stream"),
};
// some code
Ok(())
})
}
fn main() {
let addr = "127.0.0.1:8900".parse().unwrap();
handle_client(&addr);
}
【问题讨论】:
-
例如,您的代码使用了我们不知道其签名的未定义类型和方法。您的错误消息甚至与代码不对应。
-
下面的代码使用
and_then,为什么还要使用map(... write_all)?您不能随意更改您调用的方法并期望它起作用。使用and_then时,未来的成功值为(TcpStream, Vec<u8>)。 -
我不知道为什么,但是如果没有转换为
serialize_addr(remote_addr).map_err(|_| io::Error::from(io::ErrorKind::AddrNotAvailable)),我就不能使用.and_then而不是.map
标签: error-handling rust future rust-tokio