我想创建一个在实现 Tokio's 的结构上发送一些数据的方法Sink,但是我在使用Pinself. 本质上,我需要这样的东西:
fn send_data(&mut self, data: Item, cx: &mut Context) -> Poll<Result<(), Error>> {
futures_core::ready!(something.poll_ready(cx))?;
something.start_send(data)?;
futures_core::ready!(something.poll_close(cx))
}
问题是每次调用poll_ready(),start_send()和poll_close()接受self: Pin<&mut Self>,我不知道something我的用例应该是什么。如果我尝试使用let something = Pin::new(self);thensomething在调用之后会被移动,poll_ready()并且我不能将它用于后续调用(此时 self 也消失了)。我该如何解决这个问题?
use futures_core;
use std::pin::Pin;
use tokio::prelude::*; // 0.3.0-alpha.1
struct Test {}
impl Sink<i32> for Test {
type Error = ();
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn start_send(self: Pin<&mut Self>, item: i32) -> Result<(), Self::Error> {
Ok(())
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
}
impl Test {
fn send_data(&mut self, data: i32, cx: &mut Context) -> Poll<Result<(), Error>> {
// what should "something" here be?
futures_core::ready!(something.poll_ready(cx))?;
something.start_send(data)?;
futures_core::ready!(something.poll_close(cx))
}
}