微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为 Future poll_read 传递 ARC 闭包会给出 `error[E0532]: expected unit struct, unit variant or constant, found struct `tokio::io::ReadBuf``

如何解决为 Future poll_read 传递 ARC 闭包会给出 `error[E0532]: expected unit struct, unit variant or constant, found struct `tokio::io::ReadBuf``

我想传递一个在结构体的 poll_readpoll_write 上执行的闭包:

use core::task::{Context,Poll};
use std::pin::Pin;
use hyper::client::connect::{Connection,Connected};
use tokio::io::{AsyncRead,AsyncWrite};
use std::sync::Arc;


pub type OnPollRead = Arc<dyn Fn(&mut Context<'_>,&mut tokio::io::ReadBuf<'_>) -> Poll<std::io::Result<()>> + Send + Sync>;
pub type OnPollWrite = Arc<dyn Fn(&mut Context<'_>,&[u8]) -> Poll<core::result::Result<usize,std::io::Error>> + Send + Sync>;


pub struct Customresponse {
    on_poll_read: Option<OnPollRead>,on_poll_write: Option<OnPollWrite>,}

impl AsyncRead for Customresponse {
    fn poll_read(
        mut self: Pin<&mut Self>,cx: &mut Context<'_>,buf: &mut tokio::io::ReadBuf<'_>
    ) -> Poll<std::io::Result<()>> {
        (self.on_poll_read)(cx.waker().clone(),buf)
    }
}

impl AsyncWrite for Customresponse {
    fn poll_write(
        mut self: Pin<&mut Self>,buf: &[u8]
    ) -> Poll<Result<usize,std::io::Error>> {
        (self.on_poll_write)(cx.waker().clone(),buf)
    }
        fn poll_flush(
        mut self: Pin<&mut Self>,cx: &mut Context<'_>
    ) -> Poll<Result<(),std::io::Error>> {
        Poll::Ready(Ok(()))
    }

    fn poll_shutdown(
        mut self: Pin<&mut Self>,std::io::Error>>
    {
        Poll::Ready(Ok(()))
    }
}

fn main() {
    let on_poll_read = Arc::new(|&mut Context,&mut tokio::io::ReadBuf| -> Poll<std::io::Result<()>> {
        Poll::Ready(Ok(()))
    });
    let on_poll_write = Arc::new(|&mut Context,&[u8]| -> Poll<core::result::Result<usize,std::io::Error>>{
        Poll::Ready(Ok(0))
    });
}

我收到这些错误

error[E0532]: expected unit struct,unit variant or constant,found struct `tokio::io::ReadBuf`
  --> src/main.rs:47:53
   |
47 |     let on_poll_read = Arc::new(|&mut Context,&mut tokio::io::ReadBuf| -> Poll<std::io::Result<()>> {
   |                                                     ^^^^^^^^^^^^^^^^^^ help: use struct pattern Syntax instead: `tokio::io::ReadBuf { buf,filled,initialized }`
   | 
  ::: /root/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/io/read_buf.rs:27:1
   |
27 | pub struct ReadBuf<'a> {
   | ---------------------- `tokio::io::ReadBuf` defined here

error[E0277]: the size for values of type `dyn for<'r,'s,'t0> Fn(&'r mut Context<'s>,&'t0 [u8]) -> Poll<std::result::Result<usize,std::io::Error>> + Send + Sync` cannot be kNown at compilation time
  --> src/main.rs:50:25
   |
50 |     let on_poll_write = Arc::new(|&mut Context,std::io::Error>>{
   |                         ^^^^^^^^ doesn't have a size kNown at compile-time
   |
   = help: the trait `Sized` is not implemented for `dyn for<'r,std::io::Error>> + Send + Sync`
   = note: required by `Arc::<T>::new`

error[E0618]: expected function,found enum variant `(self.on_poll_read)`
  --> src/custom_transporter.rs:54:9
   |
54 |         (self.on_poll_read)(cx.waker().clone(),buf)
   |         ^^^^^^^^^^^^^^^^^^^-------------------------
   |         |
   |         call expression requires function
   |
help: `(self.on_poll_read)` is a unit variant,you need to write it without the parenthesis
   |
54 |         (self.on_poll_read)
   |

我试过 help: (self.on_poll_read) is a unit variant,you need to write it without the parenthesis,但没有括号,我得到

error[E0599]: no method named `on_poll_write` found for struct `Pin<&mut Customresponse>` in the current scope

这就更奇怪了。

另外,对于 expected unit struct,found structtokio::io::ReadBuf``,我不知道发生了什么

这是一个错误的操场:https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d2dbc27980f9cf738947b8102cdde812

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。