我有一个类型,其语义上的行为类似于流,但实际上并未实现该futures_util::Stream
特征。相反,它有一个方法get_next
,可重复返回Future
下一个项目。
我正在尝试实现一个适配器,以便将其用作流,但遇到了一些麻烦。我创建了以下两个示例:
use futures_util::Stream;
use std::{future::Future, pin::Pin, task::Poll};
struct FutureSource {/* omitted */}
impl FutureSource {
pub fn get_next(&mut self) -> FutureString {
FutureString {
source: self,
important_state: todo!(),
}
}
}
struct FutureString<'a> {
source: &'a mut FutureSource,
important_state: String,
}
impl<'a> Future for FutureString<'a> {
type Output = String;
fn poll(self: Pin<&mut Self>, _cx: &mut std::task::Context<'_>) -> Poll<Self::Output> {
unimplemented!()
}
}
struct StreamAdapter<'a> {
future_source: &'a mut FutureSource,
future: Option<FutureString<'a>>,
}
impl<'a> Stream for StreamAdapter<'a> {
type Item = <FutureString<'a> as Future>::Output;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Option<Self::Item>> {
if self.future.is_none() {
self.future.insert(self.future_source.get_next());
}
let fut = self.future.as_mut().unwrap();
match Pin::new(fut).poll(cx) {
Poll::Ready(value) => {
self.future = None;
Poll::Ready(Some(value))
}
Poll::Pending => Poll::Pending,
}
}
}
这不能编译并出现以下错误:
error[E0499]: cannot borrow `self` as mutable more than once at a time
--> src/main.rs:41:32
|
41 | self.future.insert(self.future_source.get_next());
| ---- ------ ^^^^ second mutable borrow occurs here
| | |
| | first borrow later used by call
| first mutable borrow occurs here
error: lifetime may not live long enough
--> src/main.rs:41:32
|
33 | impl<'a> Stream for StreamAdapter<'a> {
| -- lifetime `'a` defined here
...
37 | mut self: Pin<&mut Self>,
| - let's call the lifetime of this reference `'1`
...
41 | self.future.insert(self.future_source.get_next());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ argument requires that `'1` must outlive `'a`
error[E0499]: cannot borrow `self` as mutable more than once at a time
--> src/main.rs:43:19
|
33 | impl<'a> Stream for StreamAdapter<'a> {
| -- lifetime `'a` defined here
...
41 | self.future.insert(self.future_source.get_next());
| -----------------------------
| |
| first mutable borrow occurs here
| argument requires that `self` is borrowed for `'a`
42 | }
43 | let fut = self.future.as_mut().unwrap();
| ^^^^ second mutable borrow occurs here
error[E0499]: cannot borrow `self` as mutable more than once at a time
--> src/main.rs:47:17
|
33 | impl<'a> Stream for StreamAdapter<'a> {
| -- lifetime `'a` defined here
...
41 | self.future.insert(self.future_source.get_next());
| -----------------------------
| |
| first mutable borrow occurs here
| argument requires that `self` is borrowed for `'a`
...
47 | self.future = None;
| ^^^^ second mutable borrow occurs here
我对错误的理解:
cannot borrow `self` as mutable more than once at a time
self
中的可变借用self.future_source.get_next()
具有生命周期'a
,这必然意味着借用的生命周期self
必须至少为'a
。此生命周期超出了整个函数调用的范围,这意味着我们self
根本无法再次借用。lifetime may not live long enough
:
这个对我来说有点令人困惑,但我们还是来看看:future 借用了 ,self
这'a
意味着 的引用的生命周期self
必须至少为'a
。虽然这当然是可能的,但编译器无法强制执行。我可以指定 的引用self
具有生命周期,'a
但这与特征 fn 不一致。
有没有办法在安全的 Rust 中做到这一点?还是我需要使用不安全的?或者无论我怎么做,我试图做的事情都是不合理的?