diff options
author | Bert Belder <bertbelder@gmail.com> | 2020-08-26 00:22:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-26 00:22:15 +0200 |
commit | 9bfb0df805719cb3f022a5b5d9f9d898ae954c2e (patch) | |
tree | 6c7d62d95fcbde54ebbe1035bdc74618c63cfbc0 /cli/ops/io.rs | |
parent | d0ccab7fb7dd80030d3765ca9a9af44de6ecda5a (diff) |
refactor: remove OpError, use ErrBox everywhere (#7187)
Co-authored-by: Bartek IwaĆczuk <biwanczuk@gmail.com>
Diffstat (limited to 'cli/ops/io.rs')
-rw-r--r-- | cli/ops/io.rs | 68 |
1 files changed, 32 insertions, 36 deletions
diff --git a/cli/ops/io.rs b/cli/ops/io.rs index 4d03e0ef7..6b97f542c 100644 --- a/cli/ops/io.rs +++ b/cli/ops/io.rs @@ -1,9 +1,9 @@ use super::dispatch_minimal::MinimalOp; use crate::http_util::HttpBody; -use crate::op_error::OpError; use crate::state::State; use deno_core::CoreIsolate; use deno_core::CoreIsolateState; +use deno_core::ErrBox; use deno_core::ResourceTable; use deno_core::ZeroCopyBuf; use futures::future::poll_fn; @@ -116,8 +116,8 @@ fn get_stdio_stream( } } -fn no_buffer_specified() -> OpError { - OpError::type_error("no buffer specified".to_string()) +fn no_buffer_specified() -> ErrBox { + ErrBox::type_error("no buffer specified".to_string()) } #[cfg(unix)] @@ -159,7 +159,7 @@ impl Drop for StreamResourceHolder { } impl StreamResourceHolder { - pub fn track_task(&mut self, cx: &Context) -> Result<usize, OpError> { + pub fn track_task(&mut self, cx: &Context) -> Result<usize, ErrBox> { let waker = futures::task::AtomicWaker::new(); waker.register(cx.waker()); // Its OK if it overflows @@ -200,13 +200,13 @@ impl<T: AsyncRead + Unpin> UnpinAsyncRead for T {} impl<T: AsyncWrite + Unpin> UnpinAsyncWrite for T {} /// `DenoAsyncRead` is the same as the `tokio_io::AsyncRead` trait -/// but uses an `OpError` error instead of `std::io:Error` +/// but uses an `ErrBox` error instead of `std::io:Error` pub trait DenoAsyncRead { fn poll_read( &mut self, cx: &mut Context, buf: &mut [u8], - ) -> Poll<Result<usize, OpError>>; + ) -> Poll<Result<usize, ErrBox>>; } impl DenoAsyncRead for StreamResource { @@ -214,11 +214,11 @@ impl DenoAsyncRead for StreamResource { &mut self, cx: &mut Context, buf: &mut [u8], - ) -> Poll<Result<usize, OpError>> { + ) -> Poll<Result<usize, ErrBox>> { use StreamResource::*; let f: &mut dyn UnpinAsyncRead = match self { FsFile(Some((f, _))) => f, - FsFile(None) => return Poll::Ready(Err(OpError::resource_unavailable())), + FsFile(None) => return Poll::Ready(Err(ErrBox::resource_unavailable())), Stdin(f, _) => f, TcpStream(Some(f)) => f, #[cfg(not(windows))] @@ -228,7 +228,7 @@ impl DenoAsyncRead for StreamResource { ChildStdout(f) => f, ChildStderr(f) => f, HttpBody(f) => f, - _ => return Err(OpError::bad_resource_id()).into(), + _ => return Err(ErrBox::bad_resource_id()).into(), }; let v = ready!(Pin::new(f).poll_read(cx, buf))?; Ok(v).into() @@ -260,9 +260,9 @@ pub fn op_read( std_file .read(&mut zero_copy[0]) .map(|n: usize| n as i32) - .map_err(OpError::from) + .map_err(ErrBox::from) } - Err(_) => Err(OpError::type_error( + Err(_) => Err(ErrBox::type_error( "sync read not allowed on this resource".to_string(), )), }) @@ -274,13 +274,10 @@ pub fn op_read( let mut resource_table = resource_table.borrow_mut(); let resource_holder = resource_table .get_mut::<StreamResourceHolder>(rid as u32) - .ok_or_else(OpError::bad_resource_id)?; + .ok_or_else(ErrBox::bad_resource_id)?; let mut task_tracker_id: Option<usize> = None; - let nread = match resource_holder - .resource - .poll_read(cx, &mut zero_copy) - .map_err(OpError::from) + let nread = match resource_holder.resource.poll_read(cx, &mut zero_copy) { Poll::Ready(t) => { if let Some(id) = task_tracker_id { @@ -301,17 +298,17 @@ pub fn op_read( } /// `DenoAsyncWrite` is the same as the `tokio_io::AsyncWrite` trait -/// but uses an `OpError` error instead of `std::io:Error` +/// but uses an `ErrBox` error instead of `std::io:Error` pub trait DenoAsyncWrite { fn poll_write( &mut self, cx: &mut Context, buf: &[u8], - ) -> Poll<Result<usize, OpError>>; + ) -> Poll<Result<usize, ErrBox>>; - fn poll_close(&mut self, cx: &mut Context) -> Poll<Result<(), OpError>>; + fn poll_close(&mut self, cx: &mut Context) -> Poll<Result<(), ErrBox>>; - fn poll_flush(&mut self, cx: &mut Context) -> Poll<Result<(), OpError>>; + fn poll_flush(&mut self, cx: &mut Context) -> Poll<Result<(), ErrBox>>; } impl DenoAsyncWrite for StreamResource { @@ -319,7 +316,7 @@ impl DenoAsyncWrite for StreamResource { &mut self, cx: &mut Context, buf: &[u8], - ) -> Poll<Result<usize, OpError>> { + ) -> Poll<Result<usize, ErrBox>> { use StreamResource::*; let f: &mut dyn UnpinAsyncWrite = match self { FsFile(Some((f, _))) => f, @@ -330,14 +327,14 @@ impl DenoAsyncWrite for StreamResource { ClientTlsStream(f) => f, ServerTlsStream(f) => f, ChildStdin(f) => f, - _ => return Err(OpError::bad_resource_id()).into(), + _ => return Err(ErrBox::bad_resource_id()).into(), }; let v = ready!(Pin::new(f).poll_write(cx, buf))?; Ok(v).into() } - fn poll_flush(&mut self, cx: &mut Context) -> Poll<Result<(), OpError>> { + fn poll_flush(&mut self, cx: &mut Context) -> Poll<Result<(), ErrBox>> { use StreamResource::*; let f: &mut dyn UnpinAsyncWrite = match self { FsFile(Some((f, _))) => f, @@ -348,14 +345,14 @@ impl DenoAsyncWrite for StreamResource { ClientTlsStream(f) => f, ServerTlsStream(f) => f, ChildStdin(f) => f, - _ => return Err(OpError::bad_resource_id()).into(), + _ => return Err(ErrBox::bad_resource_id()).into(), }; ready!(Pin::new(f).poll_flush(cx))?; Ok(()).into() } - fn poll_close(&mut self, _cx: &mut Context) -> Poll<Result<(), OpError>> { + fn poll_close(&mut self, _cx: &mut Context) -> Poll<Result<(), ErrBox>> { unimplemented!() } } @@ -384,9 +381,9 @@ pub fn op_write( std_file .write(&zero_copy[0]) .map(|nwritten: usize| nwritten as i32) - .map_err(OpError::from) + .map_err(ErrBox::from) } - Err(_) => Err(OpError::type_error( + Err(_) => Err(ErrBox::type_error( "sync read not allowed on this resource".to_string(), )), }) @@ -400,7 +397,7 @@ pub fn op_write( let mut resource_table = resource_table.borrow_mut(); let resource_holder = resource_table .get_mut::<StreamResourceHolder>(rid as u32) - .ok_or_else(OpError::bad_resource_id)?; + .ok_or_else(ErrBox::bad_resource_id)?; resource_holder.resource.poll_write(cx, &zero_copy) }) .await?; @@ -413,7 +410,7 @@ pub fn op_write( let mut resource_table = resource_table.borrow_mut(); let resource_holder = resource_table .get_mut::<StreamResourceHolder>(rid as u32) - .ok_or_else(OpError::bad_resource_id)?; + .ok_or_else(ErrBox::bad_resource_id)?; resource_holder.resource.poll_flush(cx) }) .await?; @@ -436,11 +433,10 @@ pub fn std_file_resource<F, T>( resource_table: &mut ResourceTable, rid: u32, mut f: F, -) -> Result<T, OpError> +) -> Result<T, ErrBox> where - F: FnMut( - Result<&mut std::fs::File, &mut StreamResource>, - ) -> Result<T, OpError>, + F: + FnMut(Result<&mut std::fs::File, &mut StreamResource>) -> Result<T, ErrBox>, { // First we look up the rid in the resource table. let mut r = resource_table.get_mut::<StreamResourceHolder>(rid); @@ -469,16 +465,16 @@ where // some operation is in-flight. resource_holder.resource = StreamResource::FsFile(Some((tokio_file, metadata))); - Err(OpError::resource_unavailable()) + Err(ErrBox::resource_unavailable()) } } } else { - Err(OpError::resource_unavailable()) + Err(ErrBox::resource_unavailable()) } } _ => f(Err(&mut resource_holder.resource)), } } else { - Err(OpError::bad_resource_id()) + Err(ErrBox::bad_resource_id()) } } |