我正在使用结构来跟踪状态并Future
为其实现特征。
use futures::stream::Select;
use std::task::Poll;
struct MyStruct<T>{
stream: Select<S, S>,
storage: Vec<T>,
keeps_track: bool
}
impl<T> Future for MyStruct<T> {
type Output = T;
fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.keeps_track {
true => return Poll::Ready(self.storage.pop().unwrap()),
false => {
let item = async {
let item = self.stream.select_next_some().await;
item
};
self.storage.push(block_on(item));
return Poll::Pending
}
};
}
}
借用检查器将没有它,因为async
块可变地借用self
并且编译器不允许self
第二次可变地借用。这是期望的行为。
我的问题是:在一个块中修改一个结构上的一个字段async
并将这个计算的结果保存到这个结构上的另一个字段的 Rust 习惯用法是什么?
添加一些上下文:stream
由其他两个流构建。一旦聚合了足够多的元素,我们希望能够保存来自stream
in的连续元素以供访问。storage
我希望这样的功能在板条箱中的某个地方,但是futures
方法futures::stream::StreamExt
并不完全适合我的用例。buffered
buffer_unordered