< 返回版块

Lexo-0 发表于 2021-09-20 22:10

Tags:异步,future,生命周期


mod test {
    use std::{pin::Pin, task::Poll};

    use futures_io::{AsyncBufRead, AsyncRead};

    //不能通过编译
    struct Body(Vec<u8>);

    impl AsyncRead for Body {
        fn poll_read(
            self: Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
            buf: &mut [u8],
        ) -> Poll<futures_io::Result<usize>> {
            todo!()
        }
    }

    impl AsyncBufRead for Body {
        fn poll_fill_buf(
            self: Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
        ) -> Poll<futures_io::Result<&[u8]>> {
            Poll::Ready(Ok(&self.0))
        }

        fn consume(self: Pin<&mut Self>, amt: usize) {
            todo!()
        }
    }
}

不能通过编译


error[E0515]: cannot return value referencing function parameter `self`
  --> src/main.rs:24:13
   |
24 |             Poll::Ready(Ok(&self.0))
   |             ^^^^^^^^^^^^^^^^----^^^^
   |             |               |
   |             |               `self` is borrowed here
   |             returns a value referencing data owned by the current function

For more information about this error, try `rustc --explain E0515`.

https://play.rust-lang.org/#:~:text=Permalink%20to%20the%20playground


Ext Link: https://play.rust-lang.org/#:~:text=Permalink%20to%20the%20playground

评论区

写评论
jason1105 2021-09-21 01:15
fn poll_fill_buf(
    self: Pin<&mut Self>,
    cx: &mut std::task::Context<'_>,
) -> Poll<futures_io::Result<&[u8]>> {
    // Poll::Ready(Ok(&self.0))
    Poll::Ready(Ok(&(Pin::into_inner(self).0)))
}
1 共 1 条评论, 1 页