为返回的结果指定错误类型(在 SinkExt.with 的上下文中)

Specifying Error type for the Result returned (in the context of SinkExt.with)

我正在尝试使用 SinkExt.with 在发送之前转换数据包字节。这是一个最小的代码片段:

let (mut sink, stream) = codec::Framed::new(serial, codec::ProtocolCodec::new()).split();

let sink = sink.with(|input: Bytes| {
    // transform input
    future::ready(Ok(input))
});

不幸的是,编译器 (1.39.0) 抱怨:

error[E0698]: type inside `async` object must be known in this context
  --> src/main.rs:68:25
   |
68 |         let sink = sink.with(|b: Bytes| {
   |                         ^^^^ cannot infer type for `E`
   |

我能理解,它缺少我要返回的 Result 的可能错误类型 (E)。我的问题是我无法理解如何正确指定它。

您可以使用 turbofish 显式指定类型:

sink.with::<_,_,_/*E*/MyCustomErrorTypeOrSomethingSimilar>(|b: Bytes| {...}).