我似乎无法让编译器让我包装 Tokio AsyncRead:
use std::io::Result;
use core::pin::Pin;
use core::task::{Context, Poll};
use tokio::io::AsyncRead;
struct Wrapper<T: AsyncRead>{
inner: T
}
impl<T: AsyncRead> AsyncRead for Wrapper<T> {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8]
) -> Poll<Result<usize>> {
self.inner.poll_read(cx, buf)
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎应该编译,但编译器抱怨我没有包含正确的特征绑定,即使poll_read可以通过 AsyncRead: Playground Link获得
error[E0599]: no method named `poll_read` found for type parameter `T` in the current scope
--> src/lib.rs:17:20
|
17 | self.inner.poll_read(cx, buf)
| ^^^^^^^^^ method not found in `T`
|
= help: items from traits can only be used if the type parameter is bounded by the trait
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
查看self签名中的poll_read:
fn poll_read(
self: Pin<&mut Self>, // Self is pinned!
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
Run Code Online (Sandbox Code Playgroud)
自我被固定,意思是poll_read只能被调用的Pin<&mut T>!self.inner是类型T,这就是编译器无法找到poll_read它的原因。为了解决这个问题,我们必须以某种方式获得对该字段的固定访问权限。
Rust文档有一整节都Pin在讨论这个问题,还有一整箱致力于解决这个问题。