diff options
Diffstat (limited to 'core/examples/http_bench_json_ops.rs')
-rw-r--r-- | core/examples/http_bench_json_ops.rs | 59 |
1 files changed, 19 insertions, 40 deletions
diff --git a/core/examples/http_bench_json_ops.rs b/core/examples/http_bench_json_ops.rs index 749f40485..6f14f558c 100644 --- a/core/examples/http_bench_json_ops.rs +++ b/core/examples/http_bench_json_ops.rs @@ -1,6 +1,7 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. use deno_core::error::AnyError; use deno_core::AsyncRefCell; +use deno_core::AsyncResult; use deno_core::CancelHandle; use deno_core::CancelTryFuture; use deno_core::JsRuntime; @@ -77,19 +78,33 @@ struct TcpStream { } impl TcpStream { - async fn read(self: Rc<Self>, buf: &mut [u8]) -> Result<usize, Error> { + async fn read( + self: Rc<Self>, + mut buf: ZeroCopyBuf, + ) -> Result<usize, AnyError> { let mut rd = RcRef::map(&self, |r| &r.rd).borrow_mut().await; let cancel = RcRef::map(self, |r| &r.cancel); - rd.read(buf).try_or_cancel(cancel).await + rd.read(&mut buf) + .try_or_cancel(cancel) + .await + .map_err(AnyError::from) } - async fn write(self: Rc<Self>, buf: &[u8]) -> Result<usize, Error> { + async fn write(self: Rc<Self>, buf: ZeroCopyBuf) -> Result<usize, AnyError> { let mut wr = RcRef::map(self, |r| &r.wr).borrow_mut().await; - wr.write(buf).await + wr.write(&buf).await.map_err(AnyError::from) } } impl Resource for TcpStream { + fn read(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> { + Box::pin(self.read(buf)) + } + + fn write(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> { + Box::pin(self.write(buf)) + } + fn close(self: Rc<Self>) { self.cancel.cancel() } @@ -109,10 +124,7 @@ impl From<tokio::net::TcpStream> for TcpStream { fn create_js_runtime() -> JsRuntime { let mut runtime = JsRuntime::new(Default::default()); runtime.register_op("listen", deno_core::op_sync(op_listen)); - runtime.register_op("close", deno_core::op_sync(op_close)); runtime.register_op("accept", deno_core::op_async(op_accept)); - runtime.register_op("read", deno_core::op_async(op_read)); - runtime.register_op("write", deno_core::op_async(op_write)); runtime.sync_ops_cache(); runtime } @@ -131,15 +143,6 @@ fn op_listen( Ok(rid) } -fn op_close( - state: &mut OpState, - rid: ResourceId, - _: (), -) -> Result<(), AnyError> { - log::debug!("close rid={}", rid); - state.resource_table.close(rid).map(|_| ()) -} - async fn op_accept( state: Rc<RefCell<OpState>>, rid: ResourceId, @@ -153,30 +156,6 @@ async fn op_accept( Ok(rid) } -async fn op_read( - state: Rc<RefCell<OpState>>, - rid: ResourceId, - mut buf: ZeroCopyBuf, -) -> Result<usize, AnyError> { - log::debug!("read rid={}", rid); - - let stream = state.borrow().resource_table.get::<TcpStream>(rid)?; - let nread = stream.read(&mut buf).await?; - Ok(nread) -} - -async fn op_write( - state: Rc<RefCell<OpState>>, - rid: ResourceId, - buf: ZeroCopyBuf, -) -> Result<usize, AnyError> { - log::debug!("write rid={}", rid); - - let stream = state.borrow().resource_table.get::<TcpStream>(rid)?; - let nwritten = stream.write(&buf).await?; - Ok(nwritten) -} - fn main() { log::set_logger(&Logger).unwrap(); log::set_max_level( |