error-handling - 如何使用问号运算符来处理 Tokio future 中的错误?

标签 error-handling rust future rust-tokio

我有一个客户处理 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键入所有嵌套的闭包/ future 。编译器抛出错误

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 错误处理,但问题是我不知道如何获取 TcpStream决赛里面.then关闭。我唯一找到的地方TcpStream在 WriteAll 结构中,但它是私有(private)的。此外,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);
}

最佳答案

长话短说:您没有使用 ?运营商。


由于您没有提供,这里是MCVE你的问题。请注意,我们不知道您的 serialize_addr 的错误类型是什么功能,所以我不得不选择一些东西:

use futures::Future;
use std::{io, net::SocketAddr};
use tokio::{io::write_all, net::TcpStream};

fn serialize_addr() -> Result<Vec<u8>, Box<dyn std::error::Error>> {
    Ok(vec![])
}

pub fn handle_client(addr: &SocketAddr) -> impl Future<Item = (), Error = io::Error> {
    TcpStream::connect(addr).and_then(|stream| {
        let addr = serialize_addr()?;
        write_all(stream, addr).then(|_result| Ok(()))
    })
}
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/lib.rs:11:20
   |
11 |         let addr = serialize_addr()?;
   |                    ^^^^^^^^^^^^^^^^^ 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/lib.rs:12:38: 14: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/lib.rs:12:38: 14:10]>`
   = note: required by `std::ops::Try::from_error`

如错误消息所述:

the ? operator can only be used in a function that returns Result or Option (or another type that implements std::ops::Try)

cannot use the ? operator in a function that returns Then<WriteAll<TcpStream, Vec<u8>>, Result<(), io::Error>, [closure]>

相反,利用 Result 的事实可以把它当作一个 future ,让它参与到功能链中。

此外,就像 Rust 中的其他地方一样,您需要有一个统一的错误类型。我选择了Box<dyn Error>为简单起见。这可以使用 map_err 来实现和 Into::into

use futures::Future;
use std::net::SocketAddr;
use tokio::{io::write_all, net::TcpStream};

type Error = Box<dyn std::error::Error>;

fn serialize_addr() -> Result<Vec<u8>, Error> {
    Ok(vec![])
}

pub fn handle_client(addr: &SocketAddr) -> impl Future<Item = (), Error = Error> {
    TcpStream::connect(addr)
        .map_err(Into::into)
        .and_then(|stream| serialize_addr().map(|addr| (stream, addr)))
        .and_then(|(stream, addr)| write_all(stream, addr).map_err(Into::into))
        .then(|_result| Ok(()))
}

将来,async/await语法将使这更容易理解。

关于error-handling - 如何使用问号运算符来处理 Tokio future 中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54921250/

相关文章:

php - 尝试在 php 中使用 require_once 时出现 fatal error

javascript - Angular 4. XHR 请求中的错误停止了链式可观察量

c++ - 立即执行 .then

php - 使用 PEAR 处理数据库。向用户显示的数据库错误。如何停止?

rust - pub(crate)函数声明语法是什么意思?什么时候使用? [复制]

rust - Rust:对一个Option字段进行突变,该Option字段的包装值未实现Copy trait

rust - 为什么打开克隆的 Rc 会引起 panic ?

java - 当等待一个线程的 Future 时,线程执行是否会继续

flutter - Flutter:如何在多个类中将函数结果用作var而不重复该函数

go - 捕捉 "bind: address already in use"