我正在尝试将 Futures Stream
实现添加到已有同步 Iterator
实现的现有结构中。为了使代码保持干燥,我想修改 Iterator
实现以调用新的 Stream
实现并等待它。
我在 the code sample for Stream::by_ref
之后对 Iterator
的实现进行了建模:
stream.by_ref().take(2)
我的实现看起来像这样:
impl Iterator for Websocket {
type Item = Message;
fn next(&mut self) -> Option<Self::Item> {
self.by_ref().take(1).wait().next().and_then(Result::ok)
}
}
但是我在多个适用项目上遇到错误:
error[E0034]: multiple applicable items in scope
--> src/websocket/websocket.rs:330:14
|
330 | self.by_ref().take(1).wait().next().and_then(Result::ok)
| ^^^^^^ multiple `by_ref` found
|
note: candidate #1 is defined in an impl of the trait `websocket::websocket::futures::Stream` for the type `websocket::websocket::Websocket`
--> src/websocket/websocket.rs:149:1
|
149 | impl Stream for Websocket {
| ^^^^^^^^^^^^^^^^^^^^^^^^^
note: candidate #2 is defined in an impl of the trait `std::iter::Iterator` for the type `websocket::websocket::Websocket`
--> src/websocket/websocket.rs:324:1
|
324 | impl Iterator for Websocket {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: candidate #3 is defined in the trait `std::io::Write`
= help: to disambiguate the method call, write `std::io::Write::by_ref(&mut self)` instead
我想从左到右调用的方法是:
如果我消除代码歧义,它看起来像这样:
impl Iterator for Websocket {
type Item = Message;
fn next(&mut self) -> Option<Self::Item> {
<Self as Stream>::take(*<Self as Stream>::by_ref(self),1).wait().next().and_then(Result::ok)
}
}
但是我得到了一个终生错误:
error[E0507]: cannot move out of borrowed content
--> src/websocket/websocket.rs:331:32
|
331 | <Self as Stream>::take(*<Self as Stream>::by_ref(self),1).wait().next().and_then(Result::ok)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot move out of borrowed content
我如何做到既明确又保留对 by_ref
的结果调用 take
的能力?
我知道有新版本的 Futures,但我无法使用它,因为我使用的其他库需要 API 与我使用的 Futures 版本兼容。
最佳答案
您可以通过不尝试移出借入值来避免移出借入值:
extern crate futures;
use futures::prelude::*;
struct Message;
struct Websocket;
impl Stream for Websocket {
type Item = Message;
type Error = ();
fn poll(&mut self) -> Result<Async<Option<Self::Item>>, Self::Error> {
Err(())
}
}
impl Iterator for Websocket {
type Item = Message;
fn next(&mut self) -> Option<Self::Item> {
let a = Stream::take(self, 1);
let mut b = a.wait();
let c = Iterator::next(&mut b);
c.and_then(Result::ok)
}
}
fn main() {}
无需调用 Stream::by_ref
,因为起始类型已经是 &mut Websocket
,所以我已将其删除。
如果需要,您可以将它们重新组合成一行:
fn next(&mut self) -> Option<Self::Item> {
Stream::take(self, 1)
.wait()
.next()
.and_then(Result::ok)
}
关于rust - 如何在不移动自身的情况下消除混合 Iterator 和 Futures 调用链的歧义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50438092/