简体   繁体   English

锈与错误链和WebSocket板条箱冲突

[英]Rust conflict with error-chain and websocket crates

I'm trying to use error-chain with the websocket crate, but running into an issue that I'm not sure how to resolve. 我正在尝试将错误链与websocket板条箱一起使用,但是遇到了不确定的问题。 WsUpgrade::accept has the following signature: WsUpgrade::accept具有以下签名:

fn accept(self) -> Result<Client<S>, (S, IoError)>

Note that the Err variant is a tuple. 请注意, Err变量是一个元组。 I want to configure error-chain to handle calls to this method, so I tried to do so as follows in the relevant consuming module: 我想配置错误链来处理对此方法的调用,因此我尝试在相关的使用模块中按以下方式进行操作:

mod errors {
    error_chain! {
        foreign_links {
            WebSock((::std::net::TcpStream, ::std::io::Error));
        }
    }
}

This results in the following error, seemingly related to the presence of the tuple. 这导致以下错误,似乎与元组的存在有关。

error: expected identifier, found `(`
  --> src/lib/websock.rs:23:21
   |
23 |             WebSock((::std::net::TcpStream, ::std::io::Error));
   |                     ^

How can I resolve this? 我该如何解决? Hopefully I've missed something simple. 希望我已经错过了一些简单的事情。

A simple solution would be to introduce a type alias: 一个简单的解决方案是引入类型别名:

mod errors {
    type WebSocketError = (::std::net::TcpStream, ::std::io::Error);
    error_chain! {
        foreign_links {
            WebSock(WebSocketError);
        }
    }
}

However, this type does not implement traits required by error-chain (such as Error and Display ), so it can't be used there. 但是,此类型不能实现错误链所需的特征(例如ErrorDisplay ),因此不能在此处使用。

I think the simplest solution is to convert the error manually using Result::map_err before passing the value to error-chain. 我认为最简单的解决方案是在将值传递给错误链之前,使用Result::map_err手动转换错误。 For example, the following construction will produce Result<Client<S>, IoError> : 例如,以下构造将产生Result<Client<S>, IoError>

x.accept().map_err(|e| e.1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM