diff options
| author | Divy Srivastava <dj.srivastava23@gmail.com> | 2022-04-20 22:09:13 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-20 18:39:13 +0200 |
| commit | 2612b6f20fc21fb92402aa9086d13a7192ae3814 (patch) | |
| tree | 59db7a916a2c8ad55000b152912fd4019f75121d /core | |
| parent | 57a8fc37fc99491fa2559694f78af52a597bc501 (diff) | |
core: introduce `resource.read_return` (#14331)
Diffstat (limited to 'core')
| -rw-r--r-- | core/examples/http_bench_json_ops.rs | 16 | ||||
| -rw-r--r-- | core/resources.rs | 12 |
2 files changed, 23 insertions, 5 deletions
diff --git a/core/examples/http_bench_json_ops.rs b/core/examples/http_bench_json_ops.rs index 2068c3b85..7c895f326 100644 --- a/core/examples/http_bench_json_ops.rs +++ b/core/examples/http_bench_json_ops.rs @@ -83,13 +83,18 @@ struct TcpStream { } impl TcpStream { - async fn read(self: Rc<Self>, mut buf: ZeroCopyBuf) -> Result<usize, Error> { + async fn read( + self: Rc<Self>, + mut buf: ZeroCopyBuf, + ) -> Result<(usize, ZeroCopyBuf), Error> { let mut rd = RcRef::map(&self, |r| &r.rd).borrow_mut().await; let cancel = RcRef::map(self, |r| &r.cancel); - rd.read(&mut buf) + let nread = rd + .read(&mut buf) .try_or_cancel(cancel) .await - .map_err(Error::from) + .map_err(Error::from)?; + Ok((nread, buf)) } async fn write(self: Rc<Self>, buf: ZeroCopyBuf) -> Result<usize, Error> { @@ -99,7 +104,10 @@ impl TcpStream { } impl Resource for TcpStream { - fn read(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> { + fn read_return( + self: Rc<Self>, + buf: ZeroCopyBuf, + ) -> AsyncResult<(usize, ZeroCopyBuf)> { Box::pin(self.read(buf)) } diff --git a/core/resources.rs b/core/resources.rs index 9a1447392..ae4ef7394 100644 --- a/core/resources.rs +++ b/core/resources.rs @@ -36,7 +36,17 @@ pub trait Resource: Any + 'static { } /// Resources may implement `read()` to be a readable stream - fn read(self: Rc<Self>, _buf: ZeroCopyBuf) -> AsyncResult<usize> { + fn read(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> { + Box::pin(async move { + let (nread, _) = self.read_return(buf).await?; + Ok(nread) + }) + } + + fn read_return( + self: Rc<Self>, + _buf: ZeroCopyBuf, + ) -> AsyncResult<(usize, ZeroCopyBuf)> { Box::pin(futures::future::err(not_supported())) } |
