diff --git a/core/bindings.rs b/core/bindings.rs index 935397480a..9bfe767c3b 100644 --- a/core/bindings.rs +++ b/core/bindings.rs @@ -611,13 +611,14 @@ fn wasm_streaming_feed( let state = state_rc.borrow(); // If message_type is not Bytes, we'll be consuming the WasmStreaming // instance, so let's also remove it from the resource table. - let wasm_streaming: Option> = match message_type { - MessageType::Bytes => state.op_state.borrow().resource_table.get(rid), - _ => state.op_state.borrow_mut().resource_table.take(rid), - }; + let wasm_streaming: Result, AnyError> = + match message_type { + MessageType::Bytes => state.op_state.borrow().resource_table.get(rid), + _ => state.op_state.borrow_mut().resource_table.take(rid), + }; match wasm_streaming { - Some(wasm_streaming) => wasm_streaming, - None => return throw_type_error(scope, "Invalid resource ID."), + Ok(wasm_streaming) => wasm_streaming, + Err(e) => return throw_type_error(scope, e.to_string()), } }; diff --git a/core/examples/http_bench_json_ops.rs b/core/examples/http_bench_json_ops.rs index 82eb18d6bc..d273c2c884 100644 --- a/core/examples/http_bench_json_ops.rs +++ b/core/examples/http_bench_json_ops.rs @@ -1,5 +1,4 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use deno_core::error::bad_resource_id; use deno_core::error::null_opbuf; use deno_core::error::AnyError; use deno_core::AsyncRefCell; @@ -140,11 +139,7 @@ fn op_close( _: (), ) -> Result<(), AnyError> { log::debug!("close rid={}", rid); - state - .resource_table - .close(rid) - .map(|_| ()) - .ok_or_else(bad_resource_id) + state.resource_table.close(rid).map(|_| ()) } async fn op_accept( @@ -154,11 +149,7 @@ async fn op_accept( ) -> Result { log::debug!("accept rid={}", rid); - let listener = state - .borrow() - .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + let listener = state.borrow().resource_table.get::(rid)?; let stream = listener.accept().await?; let rid = state.borrow_mut().resource_table.add(stream); Ok(rid) @@ -172,11 +163,7 @@ async fn op_read( let mut buf = buf.ok_or_else(null_opbuf)?; log::debug!("read rid={}", rid); - let stream = state - .borrow() - .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + let stream = state.borrow().resource_table.get::(rid)?; let nread = stream.read(&mut buf).await?; Ok(nread) } @@ -189,11 +176,7 @@ async fn op_write( let buf = buf.ok_or_else(null_opbuf)?; log::debug!("write rid={}", rid); - let stream = state - .borrow() - .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + let stream = state.borrow().resource_table.get::(rid)?; let nwritten = stream.write(&buf).await?; Ok(nwritten) } diff --git a/core/ops_builtin.rs b/core/ops_builtin.rs index b00bb9c286..459b2a967f 100644 --- a/core/ops_builtin.rs +++ b/core/ops_builtin.rs @@ -1,4 +1,3 @@ -use crate::error::bad_resource_id; use crate::error::type_error; use crate::error::AnyError; use crate::include_js_files; @@ -47,10 +46,7 @@ pub fn op_close( ) -> Result<(), AnyError> { // TODO(@AaronO): drop Option after improving type-strictness balance in serde_v8 let rid = rid.ok_or_else(|| type_error("missing or invalid `rid`"))?; - state - .resource_table - .close(rid) - .ok_or_else(bad_resource_id)?; + state.resource_table.close(rid)?; Ok(()) } diff --git a/core/resources.rs b/core/resources.rs index 37f2b8edde..c5e6684a44 100644 --- a/core/resources.rs +++ b/core/resources.rs @@ -6,6 +6,8 @@ // resources. Resources may or may not correspond to a real operating system // file descriptor (hence the different name). +use crate::error::bad_resource_id; +use crate::error::AnyError; use std::any::type_name; use std::any::Any; use std::any::TypeId; @@ -105,32 +107,43 @@ impl ResourceTable { /// Returns a reference counted pointer to the resource of type `T` with the /// given `rid`. If `rid` is not present or has a type different than `T`, /// this function returns `None`. - pub fn get(&self, rid: ResourceId) -> Option> { + pub fn get(&self, rid: ResourceId) -> Result, AnyError> { self .index .get(&rid) .and_then(|rc| rc.downcast_rc::()) .map(Clone::clone) + .ok_or_else(bad_resource_id) } - pub fn get_any(&self, rid: ResourceId) -> Option> { - self.index.get(&rid).map(Clone::clone) + pub fn get_any(&self, rid: ResourceId) -> Result, AnyError> { + self + .index + .get(&rid) + .map(Clone::clone) + .ok_or_else(bad_resource_id) } /// Removes a resource of type `T` from the resource table and returns it. /// If a resource with the given `rid` exists but its type does not match `T`, /// it is not removed from the resource table. Note that the resource's /// `close()` method is *not* called. - pub fn take(&mut self, rid: ResourceId) -> Option> { + pub fn take( + &mut self, + rid: ResourceId, + ) -> Result, AnyError> { let resource = self.get::(rid)?; self.index.remove(&rid); - Some(resource) + Ok(resource) } /// Removes a resource from the resource table and returns it. Note that the /// resource's `close()` method is *not* called. - pub fn take_any(&mut self, rid: ResourceId) -> Option> { - self.index.remove(&rid) + pub fn take_any( + &mut self, + rid: ResourceId, + ) -> Result, AnyError> { + self.index.remove(&rid).ok_or_else(bad_resource_id) } /// Removes the resource with the given `rid` from the resource table. If the @@ -139,8 +152,12 @@ impl ResourceTable { /// counted, therefore pending ops are not automatically cancelled. A resource /// may implement the `close()` method to perform clean-ups such as canceling /// ops. - pub fn close(&mut self, rid: ResourceId) -> Option<()> { - self.index.remove(&rid).map(|resource| resource.close()) + pub fn close(&mut self, rid: ResourceId) -> Result<(), AnyError> { + self + .index + .remove(&rid) + .ok_or_else(bad_resource_id) + .map(|resource| resource.close()) } /// Returns an iterator that yields a `(id, name)` pair for every resource diff --git a/ext/broadcast_channel/lib.rs b/ext/broadcast_channel/lib.rs index 3f88db162d..de6c566679 100644 --- a/ext/broadcast_channel/lib.rs +++ b/ext/broadcast_channel/lib.rs @@ -5,7 +5,6 @@ mod in_memory_broadcast_channel; pub use in_memory_broadcast_channel::InMemoryBroadcastChannel; use async_trait::async_trait; -use deno_core::error::bad_resource_id; use deno_core::error::AnyError; use deno_core::include_js_files; use deno_core::op_async; @@ -68,10 +67,7 @@ pub fn op_broadcast_unsubscribe( rid: ResourceId, _buf: (), ) -> Result<(), AnyError> { - let resource = state - .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + let resource = state.resource_table.get::(rid)?; let bc = state.borrow::(); bc.unsubscribe(&resource) } @@ -81,11 +77,7 @@ pub async fn op_broadcast_send( (rid, name): (ResourceId, String), buf: ZeroCopyBuf, ) -> Result<(), AnyError> { - let resource = state - .borrow() - .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + let resource = state.borrow().resource_table.get::(rid)?; let bc = state.borrow().borrow::().clone(); bc.send(&resource, name, buf.to_vec()).await } @@ -95,11 +87,7 @@ pub async fn op_broadcast_recv( rid: ResourceId, _buf: (), ) -> Result, AnyError> { - let resource = state - .borrow() - .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + let resource = state.borrow().resource_table.get::(rid)?; let bc = state.borrow().borrow::().clone(); bc.recv(&resource).await } diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs index e89df470aa..eb2fed6831 100644 --- a/ext/fetch/lib.rs +++ b/ext/fetch/lib.rs @@ -1,7 +1,6 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. use data_url::DataUrl; -use deno_core::error::bad_resource_id; use deno_core::error::null_opbuf; use deno_core::error::type_error; use deno_core::error::AnyError; @@ -163,10 +162,7 @@ where FP: FetchPermissions + 'static, { let client = if let Some(rid) = args.client_rid { - let r = state - .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + let r = state.resource_table.get::(rid)?; r.client.clone() } else { let client = state.borrow::(); @@ -345,8 +341,7 @@ pub async fn op_fetch_send( let request = state .borrow_mut() .resource_table - .take::(rid) - .ok_or_else(bad_resource_id)?; + .take::(rid)?; let request = Rc::try_unwrap(request) .ok() @@ -402,8 +397,7 @@ pub async fn op_fetch_request_write( let resource = state .borrow() .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + .get::(rid)?; let body = RcRef::map(&resource, |r| &r.body).borrow_mut().await; let cancel = RcRef::map(resource, |r| &r.cancel); body.send(Ok(buf)).or_cancel(cancel).await?.map_err(|_| { @@ -423,8 +417,7 @@ pub async fn op_fetch_response_read( let resource = state .borrow() .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + .get::(rid)?; let mut reader = RcRef::map(&resource, |r| &r.reader).borrow_mut().await; let cancel = RcRef::map(resource, |r| &r.cancel); let mut buf = data.clone(); diff --git a/ext/ffi/lib.rs b/ext/ffi/lib.rs index 2100a4d537..cfff59953e 100644 --- a/ext/ffi/lib.rs +++ b/ext/ffi/lib.rs @@ -329,8 +329,7 @@ fn op_ffi_call( ) -> Result { let resource = state .resource_table - .get::(args.rid) - .ok_or_else(bad_resource_id)?; + .get::(args.rid)?; let symbol = resource .symbols diff --git a/ext/http/lib.rs b/ext/http/lib.rs index eac09a1ec3..8e91423457 100644 --- a/ext/http/lib.rs +++ b/ext/http/lib.rs @@ -175,8 +175,7 @@ async fn op_http_request_next( let conn_resource = state .borrow() .resource_table - .get::(conn_rid) - .ok_or_else(bad_resource_id)?; + .get::(conn_rid)?; let cancel = RcRef::map(conn_resource.clone(), |r| &r.cancel); @@ -395,8 +394,7 @@ async fn op_http_response( let response_sender = state .borrow_mut() .resource_table - .take::(rid) - .ok_or_else(bad_resource_id)?; + .take::(rid)?; let response_sender = Rc::try_unwrap(response_sender) .ok() .expect("multiple op_http_respond ongoing"); @@ -406,8 +404,7 @@ async fn op_http_response( let conn_resource = state .borrow() .resource_table - .get::(conn_rid) - .ok_or_else(bad_resource_id)?; + .get::(conn_rid)?; let mut builder = Response::builder().status(status); @@ -444,7 +441,7 @@ async fn op_http_response( poll_fn(|cx| match conn_resource.poll(cx) { Poll::Ready(x) => { - state.borrow_mut().resource_table.close(conn_rid); + state.borrow_mut().resource_table.close(conn_rid).ok(); Poll::Ready(x) } Poll::Pending => Poll::Ready(Ok(())), @@ -465,14 +462,12 @@ async fn op_http_response_close( let resource = state .borrow_mut() .resource_table - .take::(rid) - .ok_or_else(bad_resource_id)?; + .take::(rid)?; let conn_resource = state .borrow() .resource_table - .get::(resource.conn_rid) - .ok_or_else(bad_resource_id)?; + .get::(resource.conn_rid)?; drop(resource); let r = poll_fn(|cx| match conn_resource.poll(cx) { @@ -494,14 +489,12 @@ async fn op_http_request_read( let resource = state .borrow() .resource_table - .get::(rid as u32) - .ok_or_else(bad_resource_id)?; + .get::(rid as u32)?; let conn_resource = state .borrow() .resource_table - .get::(resource.conn_rid) - .ok_or_else(bad_resource_id)?; + .get::(resource.conn_rid)?; let mut inner = RcRef::map(resource.clone(), |r| &r.inner) .borrow_mut() @@ -547,14 +540,12 @@ async fn op_http_response_write( let resource = state .borrow() .resource_table - .get::(rid as u32) - .ok_or_else(bad_resource_id)?; + .get::(rid as u32)?; let conn_resource = state .borrow() .resource_table - .get::(resource.conn_rid) - .ok_or_else(bad_resource_id)?; + .get::(resource.conn_rid)?; let mut body = RcRef::map(&resource, |r| &r.body).borrow_mut().await; @@ -598,8 +589,7 @@ async fn op_http_upgrade_websocket( let req_resource = state .borrow_mut() .resource_table - .take::(rid) - .ok_or_else(bad_resource_id)?; + .take::(rid)?; let mut inner = RcRef::map(&req_resource, |r| &r.inner).borrow_mut().await; diff --git a/ext/net/io.rs b/ext/net/io.rs index fc10d7e990..f1403679a6 100644 --- a/ext/net/io.rs +++ b/ext/net/io.rs @@ -1,9 +1,9 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. use crate::ops_tls as tls; +use deno_core::error::not_supported; use deno_core::error::null_opbuf; use deno_core::error::AnyError; -use deno_core::error::{bad_resource_id, not_supported}; use deno_core::op_async; use deno_core::AsyncMutFuture; use deno_core::AsyncRefCell; @@ -169,11 +169,7 @@ async fn op_read_async( buf: Option, ) -> Result { let buf = &mut buf.ok_or_else(null_opbuf)?; - let resource = state - .borrow() - .resource_table - .get_any(rid) - .ok_or_else(bad_resource_id)?; + let resource = state.borrow().resource_table.get_any(rid)?; let nread = if let Some(s) = resource.downcast_rc::() { s.read(buf).await? } else if let Some(s) = resource.downcast_rc::() { @@ -192,11 +188,7 @@ async fn op_write_async( buf: Option, ) -> Result { let buf = &buf.ok_or_else(null_opbuf)?; - let resource = state - .borrow() - .resource_table - .get_any(rid) - .ok_or_else(bad_resource_id)?; + let resource = state.borrow().resource_table.get_any(rid)?; let nwritten = if let Some(s) = resource.downcast_rc::() { s.write(buf).await? } else if let Some(s) = resource.downcast_rc::() { @@ -214,11 +206,7 @@ async fn op_shutdown( rid: ResourceId, _: (), ) -> Result<(), AnyError> { - let resource = state - .borrow() - .resource_table - .get_any(rid) - .ok_or_else(bad_resource_id)?; + let resource = state.borrow().resource_table.get_any(rid)?; if let Some(s) = resource.downcast_rc::() { s.shutdown().await?; } else if let Some(s) = resource.downcast_rc::() { diff --git a/ext/net/ops.rs b/ext/net/ops.rs index a0fc2179e0..7f8a97df38 100644 --- a/ext/net/ops.rs +++ b/ext/net/ops.rs @@ -107,7 +107,7 @@ async fn accept_tcp( .borrow() .resource_table .get::(rid) - .ok_or_else(|| bad_resource("Listener has been closed"))?; + .map_err(|_| bad_resource("Listener has been closed"))?; let listener = RcRef::map(&resource, |r| &r.listener) .try_borrow_mut() .ok_or_else(|| custom_error("Busy", "Another accept task is ongoing"))?; @@ -178,7 +178,7 @@ async fn receive_udp( .borrow_mut() .resource_table .get::(rid) - .ok_or_else(|| bad_resource("Socket has been closed"))?; + .map_err(|_| bad_resource("Socket has been closed"))?; let socket = RcRef::map(&resource, |r| &r.socket).borrow().await; let cancel_handle = RcRef::map(&resource, |r| &r.cancel); let (size, remote_addr) = socket @@ -246,7 +246,7 @@ where .borrow_mut() .resource_table .get::(rid) - .ok_or_else(|| bad_resource("Socket has been closed"))?; + .map_err(|_| bad_resource("Socket has been closed"))?; let socket = RcRef::map(&resource, |r| &r.socket).borrow().await; let byte_length = socket.send_to(&zero_copy, &addr).await?; Ok(byte_length) @@ -266,9 +266,7 @@ where .borrow() .resource_table .get::(rid) - .ok_or_else(|| { - custom_error("NotConnected", "Socket has been closed") - })?; + .map_err(|_| custom_error("NotConnected", "Socket has been closed"))?; let socket = RcRef::map(&resource, |r| &r.socket) .try_borrow_mut() .ok_or_else(|| custom_error("Busy", "Socket already in use"))?; diff --git a/ext/net/ops_tls.rs b/ext/net/ops_tls.rs index 14a135d7dc..b89cc40053 100644 --- a/ext/net/ops_tls.rs +++ b/ext/net/ops_tls.rs @@ -11,7 +11,6 @@ use crate::DefaultTlsOptions; use crate::NetPermissions; use crate::UnsafelyIgnoreCertificateErrors; use deno_core::error::bad_resource; -use deno_core::error::bad_resource_id; use deno_core::error::custom_error; use deno_core::error::generic_error; use deno_core::error::invalid_hostname; @@ -717,8 +716,7 @@ where let resource_rc = state .borrow_mut() .resource_table - .take::(rid) - .ok_or_else(bad_resource_id)?; + .take::(rid)?; let resource = Rc::try_unwrap(resource_rc) .expect("Only a single use of this resource should happen"); let (read_half, write_half) = resource.into_inner(); @@ -1019,7 +1017,7 @@ async fn op_accept_tls( .borrow() .resource_table .get::(rid) - .ok_or_else(|| bad_resource("Listener has been closed"))?; + .map_err(|_| bad_resource("Listener has been closed"))?; let cancel_handle = RcRef::map(&resource, |r| &r.cancel_handle); let tcp_listener = RcRef::map(&resource, |r| &r.tcp_listener) diff --git a/ext/net/ops_unix.rs b/ext/net/ops_unix.rs index 9dfcc231ec..c39252fbf4 100644 --- a/ext/net/ops_unix.rs +++ b/ext/net/ops_unix.rs @@ -87,7 +87,7 @@ pub(crate) async fn accept_unix( .borrow() .resource_table .get::(rid) - .ok_or_else(|| bad_resource("Listener has been closed"))?; + .map_err(|_| bad_resource("Listener has been closed"))?; let listener = RcRef::map(&resource, |r| &r.listener) .try_borrow_mut() .ok_or_else(|| custom_error("Busy", "Listener already in use"))?; @@ -124,7 +124,7 @@ pub(crate) async fn receive_unix_packet( .borrow() .resource_table .get::(rid) - .ok_or_else(|| bad_resource("Socket has been closed"))?; + .map_err(|_| bad_resource("Socket has been closed"))?; let socket = RcRef::map(&resource, |r| &r.socket) .try_borrow_mut() .ok_or_else(|| custom_error("Busy", "Socket already in use"))?; diff --git a/ext/web/lib.rs b/ext/web/lib.rs index 9ee1eac7b4..87ae46f2f7 100644 --- a/ext/web/lib.rs +++ b/ext/web/lib.rs @@ -3,7 +3,6 @@ mod blob; mod message_port; -use deno_core::error::bad_resource_id; use deno_core::error::range_error; use deno_core::error::type_error; use deno_core::error::AnyError; @@ -240,10 +239,7 @@ fn op_encoding_decode( ) -> Result { let DecodeOptions { rid, stream } = options; - let resource = state - .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + let resource = state.resource_table.get::(rid)?; let mut decoder = resource.decoder.borrow_mut(); let fatal = resource.fatal; diff --git a/ext/web/message_port.rs b/ext/web/message_port.rs index f73d0486ab..4af99d5014 100644 --- a/ext/web/message_port.rs +++ b/ext/web/message_port.rs @@ -2,7 +2,6 @@ use std::borrow::Cow; use std::cell::RefCell; use std::rc::Rc; -use deno_core::error::bad_resource_id; use deno_core::error::type_error; use deno_core::error::AnyError; use deno_core::ZeroCopyBuf; @@ -141,7 +140,7 @@ fn deserialize_js_transferables( let resource = state .resource_table .take::(id) - .ok_or_else(|| type_error("Invalid message port transfer"))?; + .map_err(|_| type_error("Invalid message port transfer"))?; resource.cancel.cancel(); let resource = Rc::try_unwrap(resource) .map_err(|_| type_error("Message port is not ready for transfer"))?; @@ -192,10 +191,7 @@ pub fn op_message_port_post_message( } } - let resource = state - .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + let resource = state.resource_table.get::(rid)?; resource.port.send(state, data) } @@ -208,8 +204,8 @@ pub async fn op_message_port_recv_message( let resource = { let state = state.borrow(); match state.resource_table.get::(rid) { - Some(resource) => resource, - None => return Ok(None), + Ok(resource) => resource, + Err(_) => return Ok(None), } }; let cancel = RcRef::map(resource.clone(), |r| &r.cancel); diff --git a/ext/webgpu/binding.rs b/ext/webgpu/binding.rs index d20eb97d90..daf4e718d6 100644 --- a/ext/webgpu/binding.rs +++ b/ext/webgpu/binding.rs @@ -1,6 +1,5 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use deno_core::error::bad_resource_id; use deno_core::error::AnyError; use deno_core::ResourceId; use deno_core::{OpState, Resource}; @@ -84,8 +83,7 @@ pub fn op_webgpu_create_bind_group_layout( let instance = state.borrow::(); let device_resource = state .resource_table - .get::(args.device_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.device_rid)?; let device = device_resource.0; let mut entries = vec![]; @@ -217,17 +215,14 @@ pub fn op_webgpu_create_pipeline_layout( let instance = state.borrow::(); let device_resource = state .resource_table - .get::(args.device_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.device_rid)?; let device = device_resource.0; let mut bind_group_layouts = vec![]; for rid in &args.bind_group_layouts { - let bind_group_layout = state - .resource_table - .get::(*rid) - .ok_or_else(bad_resource_id)?; + let bind_group_layout = + state.resource_table.get::(*rid)?; bind_group_layouts.push(bind_group_layout.0); } @@ -271,8 +266,7 @@ pub fn op_webgpu_create_bind_group( let instance = state.borrow::(); let device_resource = state .resource_table - .get::(args.device_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.device_rid)?; let device = device_resource.0; let mut entries = vec![]; @@ -282,26 +276,26 @@ pub fn op_webgpu_create_bind_group( binding: entry.binding, resource: match entry.kind.as_str() { "GPUSampler" => { - let sampler_resource = state - .resource_table - .get::(entry.resource) - .ok_or_else(bad_resource_id)?; + let sampler_resource = + state + .resource_table + .get::(entry.resource)?; wgpu_core::binding_model::BindingResource::Sampler(sampler_resource.0) } "GPUTextureView" => { - let texture_view_resource = state - .resource_table - .get::(entry.resource) - .ok_or_else(bad_resource_id)?; + let texture_view_resource = + state + .resource_table + .get::(entry.resource)?; wgpu_core::binding_model::BindingResource::TextureView( texture_view_resource.0, ) } "GPUBufferBinding" => { - let buffer_resource = state - .resource_table - .get::(entry.resource) - .ok_or_else(bad_resource_id)?; + let buffer_resource = + state + .resource_table + .get::(entry.resource)?; wgpu_core::binding_model::BindingResource::Buffer( wgpu_core::binding_model::BufferBinding { buffer_id: buffer_resource.0, @@ -318,8 +312,7 @@ pub fn op_webgpu_create_bind_group( let bind_group_layout = state .resource_table - .get::(args.layout) - .ok_or_else(bad_resource_id)?; + .get::(args.layout)?; let descriptor = wgpu_core::binding_model::BindGroupDescriptor { label: args.label.map(Cow::from), diff --git a/ext/webgpu/buffer.rs b/ext/webgpu/buffer.rs index 6980b63480..e42c775980 100644 --- a/ext/webgpu/buffer.rs +++ b/ext/webgpu/buffer.rs @@ -1,6 +1,5 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use deno_core::error::bad_resource_id; use deno_core::error::null_opbuf; use deno_core::error::type_error; use deno_core::error::AnyError; @@ -50,8 +49,7 @@ pub fn op_webgpu_create_buffer( let instance = state.borrow::(); let device_resource = state .resource_table - .get::(args.device_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.device_rid)?; let device = device_resource.0; let descriptor = wgpu_core::resource::BufferDescriptor { @@ -90,15 +88,12 @@ pub async fn op_webgpu_buffer_get_map_async( { let state_ = state.borrow(); let instance = state_.borrow::(); - let buffer_resource = state_ - .resource_table - .get::(args.buffer_rid) - .ok_or_else(bad_resource_id)?; + let buffer_resource = + state_.resource_table.get::(args.buffer_rid)?; let buffer = buffer_resource.0; let device_resource = state_ .resource_table - .get::(args.device_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.device_rid)?; device = device_resource.0; let boxed_sender = Box::new(sender); @@ -180,10 +175,8 @@ pub fn op_webgpu_buffer_get_mapped_range( ) -> Result { let mut zero_copy = zero_copy.ok_or_else(null_opbuf)?; let instance = state.borrow::(); - let buffer_resource = state - .resource_table - .get::(args.buffer_rid) - .ok_or_else(bad_resource_id)?; + let buffer_resource = + state.resource_table.get::(args.buffer_rid)?; let buffer = buffer_resource.0; let (slice_pointer, range_size) = @@ -220,13 +213,10 @@ pub fn op_webgpu_buffer_unmap( ) -> Result { let mapped_resource = state .resource_table - .take::(args.mapped_rid) - .ok_or_else(bad_resource_id)?; + .take::(args.mapped_rid)?; let instance = state.borrow::(); - let buffer_resource = state - .resource_table - .get::(args.buffer_rid) - .ok_or_else(bad_resource_id)?; + let buffer_resource = + state.resource_table.get::(args.buffer_rid)?; let buffer = buffer_resource.0; let slice_pointer = mapped_resource.0; diff --git a/ext/webgpu/bundle.rs b/ext/webgpu/bundle.rs index b0c161b042..9ea80b171e 100644 --- a/ext/webgpu/bundle.rs +++ b/ext/webgpu/bundle.rs @@ -1,6 +1,5 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use deno_core::error::bad_resource_id; use deno_core::error::null_opbuf; use deno_core::error::AnyError; use deno_core::ResourceId; @@ -47,8 +46,7 @@ pub fn op_webgpu_create_render_bundle_encoder( ) -> Result { let device_resource = state .resource_table - .get::(args.device_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.device_rid)?; let device = device_resource.0; let mut color_formats = vec![]; @@ -98,10 +96,10 @@ pub fn op_webgpu_render_bundle_encoder_finish( args: RenderBundleEncoderFinishArgs, _: (), ) -> Result { - let render_bundle_encoder_resource = state - .resource_table - .take::(args.render_bundle_encoder_rid) - .ok_or_else(bad_resource_id)?; + let render_bundle_encoder_resource = + state + .resource_table + .take::(args.render_bundle_encoder_rid)?; let render_bundle_encoder = Rc::try_unwrap(render_bundle_encoder_resource) .ok() .expect("unwrapping render_bundle_encoder_resource should succeed") @@ -134,14 +132,14 @@ pub fn op_webgpu_render_bundle_encoder_set_bind_group( args: RenderBundleEncoderSetBindGroupArgs, zero_copy: Option, ) -> Result { - let bind_group_resource = state - .resource_table - .get::(args.bind_group) - .ok_or_else(bad_resource_id)?; - let render_bundle_encoder_resource = state - .resource_table - .get::(args.render_bundle_encoder_rid) - .ok_or_else(bad_resource_id)?; + let bind_group_resource = + state + .resource_table + .get::(args.bind_group)?; + let render_bundle_encoder_resource = + state + .resource_table + .get::(args.render_bundle_encoder_rid)?; // I know this might look like it can be easily deduplicated, but it can not // be due to the lifetime of the args.dynamic_offsets_data slice. Because we @@ -190,10 +188,10 @@ pub fn op_webgpu_render_bundle_encoder_push_debug_group( args: RenderBundleEncoderPushDebugGroupArgs, _: (), ) -> Result { - let render_bundle_encoder_resource = state - .resource_table - .get::(args.render_bundle_encoder_rid) - .ok_or_else(bad_resource_id)?; + let render_bundle_encoder_resource = + state + .resource_table + .get::(args.render_bundle_encoder_rid)?; unsafe { let label = std::ffi::CString::new(args.group_label).unwrap(); @@ -217,10 +215,10 @@ pub fn op_webgpu_render_bundle_encoder_pop_debug_group( args: RenderBundleEncoderPopDebugGroupArgs, _: (), ) -> Result { - let render_bundle_encoder_resource = state - .resource_table - .get::(args.render_bundle_encoder_rid) - .ok_or_else(bad_resource_id)?; + let render_bundle_encoder_resource = + state + .resource_table + .get::(args.render_bundle_encoder_rid)?; wgpu_core::command::bundle_ffi::wgpu_render_bundle_pop_debug_group( &mut render_bundle_encoder_resource.0.borrow_mut(), @@ -241,10 +239,10 @@ pub fn op_webgpu_render_bundle_encoder_insert_debug_marker( args: RenderBundleEncoderInsertDebugMarkerArgs, _: (), ) -> Result { - let render_bundle_encoder_resource = state - .resource_table - .get::(args.render_bundle_encoder_rid) - .ok_or_else(bad_resource_id)?; + let render_bundle_encoder_resource = + state + .resource_table + .get::(args.render_bundle_encoder_rid)?; unsafe { let label = std::ffi::CString::new(args.marker_label).unwrap(); @@ -269,14 +267,14 @@ pub fn op_webgpu_render_bundle_encoder_set_pipeline( args: RenderBundleEncoderSetPipelineArgs, _: (), ) -> Result { - let render_pipeline_resource = state - .resource_table - .get::(args.pipeline) - .ok_or_else(bad_resource_id)?; - let render_bundle_encoder_resource = state - .resource_table - .get::(args.render_bundle_encoder_rid) - .ok_or_else(bad_resource_id)?; + let render_pipeline_resource = + state + .resource_table + .get::(args.pipeline)?; + let render_bundle_encoder_resource = + state + .resource_table + .get::(args.render_bundle_encoder_rid)?; wgpu_core::command::bundle_ffi::wgpu_render_bundle_set_pipeline( &mut render_bundle_encoder_resource.0.borrow_mut(), @@ -303,12 +301,11 @@ pub fn op_webgpu_render_bundle_encoder_set_index_buffer( ) -> Result { let buffer_resource = state .resource_table - .get::(args.buffer) - .ok_or_else(bad_resource_id)?; - let render_bundle_encoder_resource = state - .resource_table - .get::(args.render_bundle_encoder_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.buffer)?; + let render_bundle_encoder_resource = + state + .resource_table + .get::(args.render_bundle_encoder_rid)?; render_bundle_encoder_resource .0 @@ -340,12 +337,11 @@ pub fn op_webgpu_render_bundle_encoder_set_vertex_buffer( ) -> Result { let buffer_resource = state .resource_table - .get::(args.buffer) - .ok_or_else(bad_resource_id)?; - let render_bundle_encoder_resource = state - .resource_table - .get::(args.render_bundle_encoder_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.buffer)?; + let render_bundle_encoder_resource = + state + .resource_table + .get::(args.render_bundle_encoder_rid)?; wgpu_core::command::bundle_ffi::wgpu_render_bundle_set_vertex_buffer( &mut render_bundle_encoder_resource.0.borrow_mut(), @@ -373,10 +369,10 @@ pub fn op_webgpu_render_bundle_encoder_draw( args: RenderBundleEncoderDrawArgs, _: (), ) -> Result { - let render_bundle_encoder_resource = state - .resource_table - .get::(args.render_bundle_encoder_rid) - .ok_or_else(bad_resource_id)?; + let render_bundle_encoder_resource = + state + .resource_table + .get::(args.render_bundle_encoder_rid)?; wgpu_core::command::bundle_ffi::wgpu_render_bundle_draw( &mut render_bundle_encoder_resource.0.borrow_mut(), @@ -405,10 +401,10 @@ pub fn op_webgpu_render_bundle_encoder_draw_indexed( args: RenderBundleEncoderDrawIndexedArgs, _: (), ) -> Result { - let render_bundle_encoder_resource = state - .resource_table - .get::(args.render_bundle_encoder_rid) - .ok_or_else(bad_resource_id)?; + let render_bundle_encoder_resource = + state + .resource_table + .get::(args.render_bundle_encoder_rid)?; wgpu_core::command::bundle_ffi::wgpu_render_bundle_draw_indexed( &mut render_bundle_encoder_resource.0.borrow_mut(), @@ -437,12 +433,11 @@ pub fn op_webgpu_render_bundle_encoder_draw_indirect( ) -> Result { let buffer_resource = state .resource_table - .get::(args.indirect_buffer) - .ok_or_else(bad_resource_id)?; - let render_bundle_encoder_resource = state - .resource_table - .get::(args.render_bundle_encoder_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.indirect_buffer)?; + let render_bundle_encoder_resource = + state + .resource_table + .get::(args.render_bundle_encoder_rid)?; wgpu_core::command::bundle_ffi::wgpu_render_bundle_draw_indirect( &mut render_bundle_encoder_resource.0.borrow_mut(), diff --git a/ext/webgpu/command_encoder.rs b/ext/webgpu/command_encoder.rs index 6821a2954e..b2d659f138 100644 --- a/ext/webgpu/command_encoder.rs +++ b/ext/webgpu/command_encoder.rs @@ -1,6 +1,5 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use deno_core::error::bad_resource_id; use deno_core::error::AnyError; use deno_core::ResourceId; use deno_core::{OpState, Resource}; @@ -53,8 +52,7 @@ pub fn op_webgpu_create_command_encoder( let instance = state.borrow::(); let device_resource = state .resource_table - .get::(args.device_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.device_rid)?; let device = device_resource.0; let descriptor = wgpu_types::CommandEncoderDescriptor { @@ -109,16 +107,15 @@ pub fn op_webgpu_command_encoder_begin_render_pass( ) -> Result { let command_encoder_resource = state .resource_table - .get::(args.command_encoder_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.command_encoder_rid)?; let mut color_attachments = vec![]; for color_attachment in args.color_attachments { - let texture_view_resource = state - .resource_table - .get::(color_attachment.view) - .ok_or_else(bad_resource_id)?; + let texture_view_resource = + state + .resource_table + .get::(color_attachment.view)?; let attachment = wgpu_core::command::RenderPassColorAttachment { view: texture_view_resource.0, @@ -128,7 +125,6 @@ pub fn op_webgpu_command_encoder_begin_render_pass( state .resource_table .get::(rid) - .ok_or_else(bad_resource_id) }) .transpose()? .map(|texture| texture.0), @@ -167,10 +163,10 @@ pub fn op_webgpu_command_encoder_begin_render_pass( let mut depth_stencil_attachment = None; if let Some(attachment) = args.depth_stencil_attachment { - let texture_view_resource = state - .resource_table - .get::(attachment.view) - .ok_or_else(bad_resource_id)?; + let texture_view_resource = + state + .resource_table + .get::(attachment.view)?; depth_stencil_attachment = Some(wgpu_core::command::RenderPassDepthStencilAttachment { @@ -242,8 +238,7 @@ pub fn op_webgpu_command_encoder_begin_compute_pass( ) -> Result { let command_encoder_resource = state .resource_table - .get::(args.command_encoder_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.command_encoder_rid)?; let descriptor = wgpu_core::command::ComputePassDescriptor { label: args.label.map(Cow::from), @@ -282,18 +277,17 @@ pub fn op_webgpu_command_encoder_copy_buffer_to_buffer( let instance = state.borrow::(); let command_encoder_resource = state .resource_table - .get::(args.command_encoder_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.command_encoder_rid)?; let command_encoder = command_encoder_resource.0; - let source_buffer_resource = state - .resource_table - .get::(args.source) - .ok_or_else(bad_resource_id)?; + let source_buffer_resource = + state + .resource_table + .get::(args.source)?; let source_buffer = source_buffer_resource.0; - let destination_buffer_resource = state - .resource_table - .get::(args.destination) - .ok_or_else(bad_resource_id)?; + let destination_buffer_resource = + state + .resource_table + .get::(args.destination)?; let destination_buffer = destination_buffer_resource.0; gfx_ok!(command_encoder => instance.command_encoder_copy_buffer_to_buffer( @@ -349,17 +343,16 @@ pub fn op_webgpu_command_encoder_copy_buffer_to_texture( let instance = state.borrow::(); let command_encoder_resource = state .resource_table - .get::(args.command_encoder_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.command_encoder_rid)?; let command_encoder = command_encoder_resource.0; - let source_buffer_resource = state - .resource_table - .get::(args.source.buffer) - .ok_or_else(bad_resource_id)?; - let destination_texture_resource = state - .resource_table - .get::(args.destination.texture) - .ok_or_else(bad_resource_id)?; + let source_buffer_resource = + state + .resource_table + .get::(args.source.buffer)?; + let destination_texture_resource = + state + .resource_table + .get::(args.destination.texture)?; let source = wgpu_core::command::ImageCopyBuffer { buffer: source_buffer_resource.0, @@ -410,17 +403,16 @@ pub fn op_webgpu_command_encoder_copy_texture_to_buffer( let instance = state.borrow::(); let command_encoder_resource = state .resource_table - .get::(args.command_encoder_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.command_encoder_rid)?; let command_encoder = command_encoder_resource.0; - let source_texture_resource = state - .resource_table - .get::(args.source.texture) - .ok_or_else(bad_resource_id)?; - let destination_buffer_resource = state - .resource_table - .get::(args.destination.buffer) - .ok_or_else(bad_resource_id)?; + let source_texture_resource = + state + .resource_table + .get::(args.source.texture)?; + let destination_buffer_resource = + state + .resource_table + .get::(args.destination.buffer)?; let source = wgpu_core::command::ImageCopyTexture { texture: source_texture_resource.0, @@ -474,17 +466,16 @@ pub fn op_webgpu_command_encoder_copy_texture_to_texture( let instance = state.borrow::(); let command_encoder_resource = state .resource_table - .get::(args.command_encoder_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.command_encoder_rid)?; let command_encoder = command_encoder_resource.0; - let source_texture_resource = state - .resource_table - .get::(args.source.texture) - .ok_or_else(bad_resource_id)?; - let destination_texture_resource = state - .resource_table - .get::(args.destination.texture) - .ok_or_else(bad_resource_id)?; + let source_texture_resource = + state + .resource_table + .get::(args.source.texture)?; + let destination_texture_resource = + state + .resource_table + .get::(args.destination.texture)?; let source = wgpu_core::command::ImageCopyTexture { texture: source_texture_resource.0, @@ -536,8 +527,7 @@ pub fn op_webgpu_command_encoder_push_debug_group( let instance = state.borrow::(); let command_encoder_resource = state .resource_table - .get::(args.command_encoder_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.command_encoder_rid)?; let command_encoder = command_encoder_resource.0; gfx_ok!(command_encoder => instance @@ -558,8 +548,7 @@ pub fn op_webgpu_command_encoder_pop_debug_group( let instance = state.borrow::(); let command_encoder_resource = state .resource_table - .get::(args.command_encoder_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.command_encoder_rid)?; let command_encoder = command_encoder_resource.0; gfx_ok!(command_encoder => instance.command_encoder_pop_debug_group(command_encoder)) @@ -580,8 +569,7 @@ pub fn op_webgpu_command_encoder_insert_debug_marker( let instance = state.borrow::(); let command_encoder_resource = state .resource_table - .get::(args.command_encoder_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.command_encoder_rid)?; let command_encoder = command_encoder_resource.0; gfx_ok!(command_encoder => instance.command_encoder_insert_debug_marker( @@ -606,13 +594,11 @@ pub fn op_webgpu_command_encoder_write_timestamp( let instance = state.borrow::(); let command_encoder_resource = state .resource_table - .get::(args.command_encoder_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.command_encoder_rid)?; let command_encoder = command_encoder_resource.0; let query_set_resource = state .resource_table - .get::(args.query_set) - .ok_or_else(bad_resource_id)?; + .get::(args.query_set)?; gfx_ok!(command_encoder => instance.command_encoder_write_timestamp( command_encoder, @@ -640,17 +626,14 @@ pub fn op_webgpu_command_encoder_resolve_query_set( let instance = state.borrow::(); let command_encoder_resource = state .resource_table - .get::(args.command_encoder_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.command_encoder_rid)?; let command_encoder = command_encoder_resource.0; let query_set_resource = state .resource_table - .get::(args.query_set) - .ok_or_else(bad_resource_id)?; + .get::(args.query_set)?; let destination_resource = state .resource_table - .get::(args.destination) - .ok_or_else(bad_resource_id)?; + .get::(args.destination)?; gfx_ok!(command_encoder => instance.command_encoder_resolve_query_set( command_encoder, @@ -676,8 +659,7 @@ pub fn op_webgpu_command_encoder_finish( ) -> Result { let command_encoder_resource = state .resource_table - .take::(args.command_encoder_rid) - .ok_or_else(bad_resource_id)?; + .take::(args.command_encoder_rid)?; let command_encoder = command_encoder_resource.0; let instance = state.borrow::(); diff --git a/ext/webgpu/compute_pass.rs b/ext/webgpu/compute_pass.rs index 6971dbe5cb..4fc0af5385 100644 --- a/ext/webgpu/compute_pass.rs +++ b/ext/webgpu/compute_pass.rs @@ -1,6 +1,5 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use deno_core::error::bad_resource_id; use deno_core::error::null_opbuf; use deno_core::error::AnyError; use deno_core::ResourceId; @@ -33,14 +32,13 @@ pub fn op_webgpu_compute_pass_set_pipeline( args: ComputePassSetPipelineArgs, _: (), ) -> Result { - let compute_pipeline_resource = state - .resource_table - .get::(args.pipeline) - .ok_or_else(bad_resource_id)?; + let compute_pipeline_resource = + state + .resource_table + .get::(args.pipeline)?; let compute_pass_resource = state .resource_table - .get::(args.compute_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.compute_pass_rid)?; wgpu_core::command::compute_ffi::wgpu_compute_pass_set_pipeline( &mut compute_pass_resource.0.borrow_mut(), @@ -66,8 +64,7 @@ pub fn op_webgpu_compute_pass_dispatch( ) -> Result { let compute_pass_resource = state .resource_table - .get::(args.compute_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.compute_pass_rid)?; wgpu_core::command::compute_ffi::wgpu_compute_pass_dispatch( &mut compute_pass_resource.0.borrow_mut(), @@ -94,12 +91,10 @@ pub fn op_webgpu_compute_pass_dispatch_indirect( ) -> Result { let buffer_resource = state .resource_table - .get::(args.indirect_buffer) - .ok_or_else(bad_resource_id)?; + .get::(args.indirect_buffer)?; let compute_pass_resource = state .resource_table - .get::(args.compute_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.compute_pass_rid)?; wgpu_core::command::compute_ffi::wgpu_compute_pass_dispatch_indirect( &mut compute_pass_resource.0.borrow_mut(), @@ -125,12 +120,10 @@ pub fn op_webgpu_compute_pass_begin_pipeline_statistics_query( ) -> Result { let compute_pass_resource = state .resource_table - .get::(args.compute_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.compute_pass_rid)?; let query_set_resource = state .resource_table - .get::(args.query_set) - .ok_or_else(bad_resource_id)?; + .get::(args.query_set)?; wgpu_core::command::compute_ffi::wgpu_compute_pass_begin_pipeline_statistics_query( &mut compute_pass_resource.0.borrow_mut(), @@ -154,8 +147,7 @@ pub fn op_webgpu_compute_pass_end_pipeline_statistics_query( ) -> Result { let compute_pass_resource = state .resource_table - .get::(args.compute_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.compute_pass_rid)?; wgpu_core::command::compute_ffi::wgpu_compute_pass_end_pipeline_statistics_query( &mut compute_pass_resource.0.borrow_mut(), @@ -179,12 +171,10 @@ pub fn op_webgpu_compute_pass_write_timestamp( ) -> Result { let compute_pass_resource = state .resource_table - .get::(args.compute_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.compute_pass_rid)?; let query_set_resource = state .resource_table - .get::(args.query_set) - .ok_or_else(bad_resource_id)?; + .get::(args.query_set)?; wgpu_core::command::compute_ffi::wgpu_compute_pass_write_timestamp( &mut compute_pass_resource.0.borrow_mut(), @@ -210,14 +200,12 @@ pub fn op_webgpu_compute_pass_end_pass( let command_encoder_resource = state .resource_table .get::( - args.command_encoder_rid, - ) - .ok_or_else(bad_resource_id)?; + args.command_encoder_rid, + )?; let command_encoder = command_encoder_resource.0; let compute_pass_resource = state .resource_table - .take::(args.compute_pass_rid) - .ok_or_else(bad_resource_id)?; + .take::(args.compute_pass_rid)?; let compute_pass = &compute_pass_resource.0.borrow(); let instance = state.borrow::(); @@ -243,14 +231,13 @@ pub fn op_webgpu_compute_pass_set_bind_group( args: ComputePassSetBindGroupArgs, zero_copy: Option, ) -> Result { - let bind_group_resource = state - .resource_table - .get::(args.bind_group) - .ok_or_else(bad_resource_id)?; + let bind_group_resource = + state + .resource_table + .get::(args.bind_group)?; let compute_pass_resource = state .resource_table - .get::(args.compute_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.compute_pass_rid)?; unsafe { wgpu_core::command::compute_ffi::wgpu_compute_pass_set_bind_group( @@ -288,8 +275,7 @@ pub fn op_webgpu_compute_pass_push_debug_group( ) -> Result { let compute_pass_resource = state .resource_table - .get::(args.compute_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.compute_pass_rid)?; unsafe { let label = std::ffi::CString::new(args.group_label).unwrap(); @@ -316,8 +302,7 @@ pub fn op_webgpu_compute_pass_pop_debug_group( ) -> Result { let compute_pass_resource = state .resource_table - .get::(args.compute_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.compute_pass_rid)?; wgpu_core::command::compute_ffi::wgpu_compute_pass_pop_debug_group( &mut compute_pass_resource.0.borrow_mut(), @@ -340,8 +325,7 @@ pub fn op_webgpu_compute_pass_insert_debug_marker( ) -> Result { let compute_pass_resource = state .resource_table - .get::(args.compute_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.compute_pass_rid)?; unsafe { let label = std::ffi::CString::new(args.marker_label).unwrap(); diff --git a/ext/webgpu/lib.rs b/ext/webgpu/lib.rs index 9a39ea4a2e..aa9de21109 100644 --- a/ext/webgpu/lib.rs +++ b/ext/webgpu/lib.rs @@ -1,7 +1,7 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. +use deno_core::error::not_supported; use deno_core::error::AnyError; -use deno_core::error::{bad_resource_id, not_supported}; use deno_core::include_js_files; use deno_core::op_async; use deno_core::op_sync; @@ -398,8 +398,7 @@ pub async fn op_webgpu_request_device( let mut state = state.borrow_mut(); let adapter_resource = state .resource_table - .get::(args.adapter_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.adapter_rid)?; let adapter = adapter_resource.0; let instance = state.borrow::(); @@ -533,10 +532,8 @@ pub fn op_webgpu_create_query_set( args: CreateQuerySetArgs, _: (), ) -> Result { - let device_resource = state - .resource_table - .get::(args.device_rid) - .ok_or_else(bad_resource_id)?; + let device_resource = + state.resource_table.get::(args.device_rid)?; let device = device_resource.0; let instance = &state.borrow::(); diff --git a/ext/webgpu/pipeline.rs b/ext/webgpu/pipeline.rs index 6d11179a4f..3a3707d366 100644 --- a/ext/webgpu/pipeline.rs +++ b/ext/webgpu/pipeline.rs @@ -1,6 +1,5 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use deno_core::error::bad_resource_id; use deno_core::error::AnyError; use deno_core::ResourceId; use deno_core::{OpState, Resource}; @@ -173,24 +172,20 @@ pub fn op_webgpu_create_compute_pipeline( let instance = state.borrow::(); let device_resource = state .resource_table - .get::(args.device_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.device_rid)?; let device = device_resource.0; let pipeline_layout = if let Some(rid) = args.layout { - let id = state - .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + let id = state.resource_table.get::(rid)?; Some(id.0) } else { None }; - let compute_shader_module_resource = state - .resource_table - .get::(args.compute.module) - .ok_or_else(bad_resource_id)?; + let compute_shader_module_resource = + state + .resource_table + .get::(args.compute.module)?; let descriptor = wgpu_core::pipeline::ComputePipelineDescriptor { label: args.label.map(Cow::from), @@ -246,8 +241,7 @@ pub fn op_webgpu_compute_pipeline_get_bind_group_layout( let instance = state.borrow::(); let compute_pipeline_resource = state .resource_table - .get::(args.compute_pipeline_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.compute_pipeline_rid)?; let compute_pipeline = compute_pipeline_resource.0; let (bind_group_layout, maybe_err) = gfx_select!(compute_pipeline => instance.compute_pipeline_get_bind_group_layout(compute_pipeline, args.index, std::marker::PhantomData)); @@ -464,24 +458,21 @@ pub fn op_webgpu_create_render_pipeline( let instance = state.borrow::(); let device_resource = state .resource_table - .get::(args.device_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.device_rid)?; let device = device_resource.0; let layout = if let Some(rid) = args.layout { - let pipeline_layout_resource = state - .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + let pipeline_layout_resource = + state.resource_table.get::(rid)?; Some(pipeline_layout_resource.0) } else { None }; - let vertex_shader_module_resource = state - .resource_table - .get::(args.vertex.module) - .ok_or_else(bad_resource_id)?; + let vertex_shader_module_resource = + state + .resource_table + .get::(args.vertex.module)?; let descriptor = wgpu_core::pipeline::RenderPipelineDescriptor { label: args.label.map(Cow::from), @@ -601,7 +592,6 @@ pub fn op_webgpu_create_render_pipeline( let fragment_shader_module_resource = state .resource_table .get::(fragment.module) - .ok_or_else(bad_resource_id) .unwrap(); wgpu_core::pipeline::FragmentState { @@ -666,8 +656,7 @@ pub fn op_webgpu_render_pipeline_get_bind_group_layout( let instance = state.borrow::(); let render_pipeline_resource = state .resource_table - .get::(args.render_pipeline_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pipeline_rid)?; let render_pipeline = render_pipeline_resource.0; let (bind_group_layout, maybe_err) = gfx_select!(render_pipeline => instance.render_pipeline_get_bind_group_layout(render_pipeline, args.index, std::marker::PhantomData)); diff --git a/ext/webgpu/queue.rs b/ext/webgpu/queue.rs index 876c2a207e..6af1e61e39 100644 --- a/ext/webgpu/queue.rs +++ b/ext/webgpu/queue.rs @@ -2,7 +2,6 @@ use std::num::NonZeroU32; -use deno_core::error::bad_resource_id; use deno_core::error::null_opbuf; use deno_core::error::AnyError; use deno_core::OpState; @@ -27,19 +26,17 @@ pub fn op_webgpu_queue_submit( _: (), ) -> Result { let instance = state.borrow::(); - let queue_resource = state - .resource_table - .get::(args.queue_rid) - .ok_or_else(bad_resource_id)?; + let queue_resource = + state.resource_table.get::(args.queue_rid)?; let queue = queue_resource.0; let mut ids = vec![]; for rid in args.command_buffers { - let buffer_resource = state - .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + let buffer_resource = + state + .resource_table + .get::(rid)?; ids.push(buffer_resource.0); } @@ -76,13 +73,10 @@ pub fn op_webgpu_write_buffer( let instance = state.borrow::(); let buffer_resource = state .resource_table - .get::(args.buffer) - .ok_or_else(bad_resource_id)?; + .get::(args.buffer)?; let buffer = buffer_resource.0; - let queue_resource = state - .resource_table - .get::(args.queue_rid) - .ok_or_else(bad_resource_id)?; + let queue_resource = + state.resource_table.get::(args.queue_rid)?; let queue = queue_resource.0; let data = match args.size { @@ -118,12 +112,9 @@ pub fn op_webgpu_write_texture( let instance = state.borrow::(); let texture_resource = state .resource_table - .get::(args.destination.texture) - .ok_or_else(bad_resource_id)?; - let queue_resource = state - .resource_table - .get::(args.queue_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.destination.texture)?; + let queue_resource = + state.resource_table.get::(args.queue_rid)?; let queue = queue_resource.0; let destination = wgpu_core::command::ImageCopyTexture { diff --git a/ext/webgpu/render_pass.rs b/ext/webgpu/render_pass.rs index 81f2e6640e..e01a6d478f 100644 --- a/ext/webgpu/render_pass.rs +++ b/ext/webgpu/render_pass.rs @@ -1,6 +1,5 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use deno_core::error::bad_resource_id; use deno_core::error::null_opbuf; use deno_core::error::type_error; use deno_core::error::AnyError; @@ -41,8 +40,7 @@ pub fn op_webgpu_render_pass_set_viewport( ) -> Result { let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; wgpu_core::command::render_ffi::wgpu_render_pass_set_viewport( &mut render_pass_resource.0.borrow_mut(), @@ -74,8 +72,7 @@ pub fn op_webgpu_render_pass_set_scissor_rect( ) -> Result { let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; wgpu_core::command::render_ffi::wgpu_render_pass_set_scissor_rect( &mut render_pass_resource.0.borrow_mut(), @@ -111,8 +108,7 @@ pub fn op_webgpu_render_pass_set_blend_constant( ) -> Result { let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; wgpu_core::command::render_ffi::wgpu_render_pass_set_blend_constant( &mut render_pass_resource.0.borrow_mut(), @@ -141,8 +137,7 @@ pub fn op_webgpu_render_pass_set_stencil_reference( ) -> Result { let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; wgpu_core::command::render_ffi::wgpu_render_pass_set_stencil_reference( &mut render_pass_resource.0.borrow_mut(), @@ -167,12 +162,10 @@ pub fn op_webgpu_render_pass_begin_pipeline_statistics_query( ) -> Result { let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; let query_set_resource = state .resource_table - .get::(args.query_set) - .ok_or_else(bad_resource_id)?; + .get::(args.query_set)?; wgpu_core::command::render_ffi::wgpu_render_pass_begin_pipeline_statistics_query( &mut render_pass_resource.0.borrow_mut(), @@ -196,8 +189,7 @@ pub fn op_webgpu_render_pass_end_pipeline_statistics_query( ) -> Result { let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; wgpu_core::command::render_ffi::wgpu_render_pass_end_pipeline_statistics_query( &mut render_pass_resource.0.borrow_mut(), @@ -221,12 +213,10 @@ pub fn op_webgpu_render_pass_write_timestamp( ) -> Result { let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; let query_set_resource = state .resource_table - .get::(args.query_set) - .ok_or_else(bad_resource_id)?; + .get::(args.query_set)?; wgpu_core::command::render_ffi::wgpu_render_pass_write_timestamp( &mut render_pass_resource.0.borrow_mut(), @@ -252,17 +242,16 @@ pub fn op_webgpu_render_pass_execute_bundles( let mut render_bundle_ids = vec![]; for rid in &args.bundles { - let render_bundle_resource = state - .resource_table - .get::(*rid) - .ok_or_else(bad_resource_id)?; + let render_bundle_resource = + state + .resource_table + .get::(*rid)?; render_bundle_ids.push(render_bundle_resource.0); } let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; unsafe { wgpu_core::command::render_ffi::wgpu_render_pass_execute_bundles( @@ -290,14 +279,12 @@ pub fn op_webgpu_render_pass_end_pass( let command_encoder_resource = state .resource_table .get::( - args.command_encoder_rid, - ) - .ok_or_else(bad_resource_id)?; + args.command_encoder_rid, + )?; let command_encoder = command_encoder_resource.0; let render_pass_resource = state .resource_table - .take::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .take::(args.render_pass_rid)?; let render_pass = &render_pass_resource.0.borrow(); let instance = state.borrow::(); @@ -320,14 +307,13 @@ pub fn op_webgpu_render_pass_set_bind_group( args: RenderPassSetBindGroupArgs, zero_copy: Option, ) -> Result { - let bind_group_resource = state - .resource_table - .get::(args.bind_group) - .ok_or_else(bad_resource_id)?; + let bind_group_resource = + state + .resource_table + .get::(args.bind_group)?; let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; // I know this might look like it can be easily deduplicated, but it can not // be due to the lifetime of the args.dynamic_offsets_data slice. Because we @@ -378,8 +364,7 @@ pub fn op_webgpu_render_pass_push_debug_group( ) -> Result { let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; unsafe { let label = std::ffi::CString::new(args.group_label).unwrap(); @@ -406,8 +391,7 @@ pub fn op_webgpu_render_pass_pop_debug_group( ) -> Result { let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; wgpu_core::command::render_ffi::wgpu_render_pass_pop_debug_group( &mut render_pass_resource.0.borrow_mut(), @@ -430,8 +414,7 @@ pub fn op_webgpu_render_pass_insert_debug_marker( ) -> Result { let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; unsafe { let label = std::ffi::CString::new(args.marker_label).unwrap(); @@ -457,14 +440,13 @@ pub fn op_webgpu_render_pass_set_pipeline( args: RenderPassSetPipelineArgs, _: (), ) -> Result { - let render_pipeline_resource = state - .resource_table - .get::(args.pipeline) - .ok_or_else(bad_resource_id)?; + let render_pipeline_resource = + state + .resource_table + .get::(args.pipeline)?; let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; wgpu_core::command::render_ffi::wgpu_render_pass_set_pipeline( &mut render_pass_resource.0.borrow_mut(), @@ -491,12 +473,10 @@ pub fn op_webgpu_render_pass_set_index_buffer( ) -> Result { let buffer_resource = state .resource_table - .get::(args.buffer) - .ok_or_else(bad_resource_id)?; + .get::(args.buffer)?; let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; let size = if let Some(size) = args.size { Some( @@ -534,12 +514,10 @@ pub fn op_webgpu_render_pass_set_vertex_buffer( ) -> Result { let buffer_resource = state .resource_table - .get::(args.buffer) - .ok_or_else(bad_resource_id)?; + .get::(args.buffer)?; let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; let size = if let Some(size) = args.size { Some( @@ -578,8 +556,7 @@ pub fn op_webgpu_render_pass_draw( ) -> Result { let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; wgpu_core::command::render_ffi::wgpu_render_pass_draw( &mut render_pass_resource.0.borrow_mut(), @@ -610,8 +587,7 @@ pub fn op_webgpu_render_pass_draw_indexed( ) -> Result { let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; wgpu_core::command::render_ffi::wgpu_render_pass_draw_indexed( &mut render_pass_resource.0.borrow_mut(), @@ -640,12 +616,10 @@ pub fn op_webgpu_render_pass_draw_indirect( ) -> Result { let buffer_resource = state .resource_table - .get::(args.indirect_buffer) - .ok_or_else(bad_resource_id)?; + .get::(args.indirect_buffer)?; let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; wgpu_core::command::render_ffi::wgpu_render_pass_draw_indirect( &mut render_pass_resource.0.borrow_mut(), @@ -671,12 +645,10 @@ pub fn op_webgpu_render_pass_draw_indexed_indirect( ) -> Result { let buffer_resource = state .resource_table - .get::(args.indirect_buffer) - .ok_or_else(bad_resource_id)?; + .get::(args.indirect_buffer)?; let render_pass_resource = state .resource_table - .get::(args.render_pass_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.render_pass_rid)?; wgpu_core::command::render_ffi::wgpu_render_pass_draw_indexed_indirect( &mut render_pass_resource.0.borrow_mut(), diff --git a/ext/webgpu/sampler.rs b/ext/webgpu/sampler.rs index 7921c41518..827ba4b3a4 100644 --- a/ext/webgpu/sampler.rs +++ b/ext/webgpu/sampler.rs @@ -1,6 +1,5 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use deno_core::error::bad_resource_id; use deno_core::error::AnyError; use deno_core::ResourceId; use deno_core::{OpState, Resource}; @@ -84,8 +83,7 @@ pub fn op_webgpu_create_sampler( let instance = state.borrow::(); let device_resource = state .resource_table - .get::(args.device_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.device_rid)?; let device = device_resource.0; let descriptor = wgpu_core::resource::SamplerDescriptor { diff --git a/ext/webgpu/shader.rs b/ext/webgpu/shader.rs index f48411969c..bd3b03f55d 100644 --- a/ext/webgpu/shader.rs +++ b/ext/webgpu/shader.rs @@ -1,6 +1,5 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use deno_core::error::bad_resource_id; use deno_core::error::null_opbuf; use deno_core::error::AnyError; use deno_core::ResourceId; @@ -35,8 +34,7 @@ pub fn op_webgpu_create_shader_module( let instance = state.borrow::(); let device_resource = state .resource_table - .get::(args.device_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.device_rid)?; let device = device_resource.0; let source = match args.code { diff --git a/ext/webgpu/texture.rs b/ext/webgpu/texture.rs index 587ac46a7a..4829811e26 100644 --- a/ext/webgpu/texture.rs +++ b/ext/webgpu/texture.rs @@ -1,7 +1,7 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. +use deno_core::error::not_supported; use deno_core::error::AnyError; -use deno_core::error::{bad_resource_id, not_supported}; use deno_core::ResourceId; use deno_core::{OpState, Resource}; use serde::Deserialize; @@ -149,8 +149,7 @@ pub fn op_webgpu_create_texture( let instance = state.borrow::(); let device_resource = state .resource_table - .get::(args.device_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.device_rid)?; let device = device_resource.0; let descriptor = wgpu_core::resource::TextureDescriptor { @@ -204,8 +203,7 @@ pub fn op_webgpu_create_texture_view( let instance = state.borrow::(); let texture_resource = state .resource_table - .get::(args.texture_rid) - .ok_or_else(bad_resource_id)?; + .get::(args.texture_rid)?; let texture = texture_resource.0; let descriptor = wgpu_core::resource::TextureViewDescriptor { diff --git a/ext/websocket/lib.rs b/ext/websocket/lib.rs index 40bcb7bc4f..c75fa17420 100644 --- a/ext/websocket/lib.rs +++ b/ext/websocket/lib.rs @@ -1,6 +1,5 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use deno_core::error::bad_resource_id; use deno_core::error::invalid_hostname; use deno_core::error::null_opbuf; use deno_core::error::AnyError; @@ -272,8 +271,7 @@ where let r = state .borrow_mut() .resource_table - .get::(cancel_rid) - .ok_or_else(bad_resource_id)?; + .get::(cancel_rid)?; client .or_cancel(r.0.to_owned()) .await @@ -289,7 +287,7 @@ where })?; if let Some(cancel_rid) = args.cancel_handle { - state.borrow_mut().resource_table.close(cancel_rid); + state.borrow_mut().resource_table.close(cancel_rid).ok(); } let (ws_tx, ws_rx) = stream.split(); @@ -343,8 +341,7 @@ pub async fn op_ws_send( let resource = state .borrow_mut() .resource_table - .get::(args.rid) - .ok_or_else(bad_resource_id)?; + .get::(args.rid)?; resource.send(msg).await?; Ok(()) } @@ -374,8 +371,7 @@ pub async fn op_ws_close( let resource = state .borrow_mut() .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + .get::(rid)?; resource.send(msg).await?; Ok(()) } @@ -400,8 +396,7 @@ pub async fn op_ws_next_event( let resource = state .borrow_mut() .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + .get::(rid)?; let cancel = RcRef::map(&resource, |r| &r.cancel); let val = resource.next_message(cancel).await?; diff --git a/runtime/ops/fs.rs b/runtime/ops/fs.rs index c7f4295ba0..419e41718f 100644 --- a/runtime/ops/fs.rs +++ b/runtime/ops/fs.rs @@ -230,8 +230,7 @@ async fn op_seek_async( let resource = state .borrow_mut() .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + .get::(rid)?; if resource.fs_file.is_none() { return Err(bad_resource_id()); @@ -265,8 +264,7 @@ async fn op_fdatasync_async( let resource = state .borrow_mut() .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + .get::(rid)?; if resource.fs_file.is_none() { return Err(bad_resource_id()); @@ -300,8 +298,7 @@ async fn op_fsync_async( let resource = state .borrow_mut() .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + .get::(rid)?; if resource.fs_file.is_none() { return Err(bad_resource_id()); @@ -335,8 +332,7 @@ async fn op_fstat_async( let resource = state .borrow_mut() .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + .get::(rid)?; if resource.fs_file.is_none() { return Err(bad_resource_id()); @@ -1298,8 +1294,7 @@ async fn op_ftruncate_async( let resource = state .borrow_mut() .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + .get::(rid)?; if resource.fs_file.is_none() { return Err(bad_resource_id()); @@ -1580,8 +1575,7 @@ async fn op_futime_async( let resource = state .borrow_mut() .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + .get::(rid)?; if resource.fs_file.is_none() { return Err(bad_resource_id()); diff --git a/runtime/ops/fs_events.rs b/runtime/ops/fs_events.rs index ead56bdc4d..987a7f62aa 100644 --- a/runtime/ops/fs_events.rs +++ b/runtime/ops/fs_events.rs @@ -1,7 +1,6 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. use crate::permissions::Permissions; -use deno_core::error::bad_resource_id; use deno_core::error::AnyError; use deno_core::parking_lot::Mutex; use deno_core::AsyncRefCell; @@ -135,11 +134,7 @@ async fn op_fs_events_poll( rid: ResourceId, _: (), ) -> Result, AnyError> { - let resource = state - .borrow() - .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + let resource = state.borrow().resource_table.get::(rid)?; let mut receiver = RcRef::map(&resource, |r| &r.receiver).borrow_mut().await; let cancel = RcRef::map(resource, |r| &r.cancel); let maybe_result = receiver.recv().or_cancel(cancel).await?; diff --git a/runtime/ops/http.rs b/runtime/ops/http.rs index 5b156fc11c..58783bbbc2 100644 --- a/runtime/ops/http.rs +++ b/runtime/ops/http.rs @@ -20,7 +20,7 @@ fn op_http_start( tcp_stream_rid: ResourceId, _: (), ) -> Result { - if let Some(resource_rc) = state + if let Ok(resource_rc) = state .resource_table .take::(tcp_stream_rid) { @@ -32,7 +32,7 @@ fn op_http_start( return deno_http::start_http(state, tcp_stream, addr, "http"); } - if let Some(resource_rc) = state + if let Ok(resource_rc) = state .resource_table .take::(tcp_stream_rid) { diff --git a/runtime/ops/io.rs b/runtime/ops/io.rs index e188464664..82fe3605cc 100644 --- a/runtime/ops/io.rs +++ b/runtime/ops/io.rs @@ -1,9 +1,9 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. +use deno_core::error::not_supported; use deno_core::error::null_opbuf; use deno_core::error::resource_unavailable; use deno_core::error::AnyError; -use deno_core::error::{bad_resource_id, not_supported}; use deno_core::op_async; use deno_core::op_sync; use deno_core::AsyncMutFuture; @@ -334,10 +334,7 @@ impl StdFileResource { F: FnMut(Result<&mut std::fs::File, ()>) -> Result, { // First we look up the rid in the resource table. - let resource = state - .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + let resource = state.resource_table.get::(rid)?; // Sync write only works for FsFile. It doesn't make sense to do this // for non-blocking sockets. So we error out if not FsFile. @@ -408,11 +405,7 @@ async fn op_read_async( buf: Option, ) -> Result { let buf = &mut buf.ok_or_else(null_opbuf)?; - let resource = state - .borrow() - .resource_table - .get_any(rid) - .ok_or_else(bad_resource_id)?; + let resource = state.borrow().resource_table.get_any(rid)?; let nread = if let Some(s) = resource.downcast_rc::() { s.read(buf).await? } else if let Some(s) = resource.downcast_rc::() { @@ -452,11 +445,7 @@ async fn op_write_async( buf: Option, ) -> Result { let buf = &buf.ok_or_else(null_opbuf)?; - let resource = state - .borrow() - .resource_table - .get_any(rid) - .ok_or_else(bad_resource_id)?; + let resource = state.borrow().resource_table.get_any(rid)?; let nwritten = if let Some(s) = resource.downcast_rc::() { s.write(buf).await? } else if let Some(s) = resource.downcast_rc::() { @@ -478,11 +467,7 @@ async fn op_shutdown( rid: ResourceId, _: (), ) -> Result<(), AnyError> { - let resource = state - .borrow() - .resource_table - .get_any(rid) - .ok_or_else(bad_resource_id)?; + let resource = state.borrow().resource_table.get_any(rid)?; if let Some(s) = resource.downcast_rc::() { s.shutdown().await?; } else if let Some(s) = resource.downcast_rc::() { diff --git a/runtime/ops/process.rs b/runtime/ops/process.rs index 0f0cc6e2a0..44ead73da3 100644 --- a/runtime/ops/process.rs +++ b/runtime/ops/process.rs @@ -212,8 +212,7 @@ async fn op_run_status( let resource = state .borrow_mut() .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + .get::(rid)?; let mut child = resource.borrow_mut().await; let run_status = child.wait().await?; let code = run_status.code(); diff --git a/runtime/ops/signal.rs b/runtime/ops/signal.rs index ed9c9797e7..8890217064 100644 --- a/runtime/ops/signal.rs +++ b/runtime/ops/signal.rs @@ -7,8 +7,6 @@ use deno_core::OpState; use std::cell::RefCell; use std::rc::Rc; -#[cfg(unix)] -use deno_core::error::bad_resource_id; #[cfg(unix)] use deno_core::AsyncRefCell; #[cfg(unix)] @@ -81,8 +79,7 @@ async fn op_signal_poll( let resource = state .borrow_mut() .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + .get::(rid)?; let cancel = RcRef::map(&resource, |r| &r.cancel); let mut signal = RcRef::map(&resource, |r| &r.signal).borrow_mut().await; @@ -99,10 +96,7 @@ pub fn op_signal_unbind( _: (), ) -> Result<(), AnyError> { super::check_unstable(state, "Deno.signal"); - state - .resource_table - .close(rid) - .ok_or_else(bad_resource_id)?; + state.resource_table.close(rid)?; Ok(()) } diff --git a/runtime/ops/tty.rs b/runtime/ops/tty.rs index e9e2e67008..8295af1c79 100644 --- a/runtime/ops/tty.rs +++ b/runtime/ops/tty.rs @@ -89,10 +89,7 @@ fn op_set_raw( use winapi::shared::minwindef::FALSE; use winapi::um::{consoleapi, handleapi}; - let resource = state - .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + let resource = state.resource_table.get::(rid)?; if cbreak { return Err(not_supported()); @@ -156,10 +153,7 @@ fn op_set_raw( { use std::os::unix::io::AsRawFd; - let resource = state - .resource_table - .get::(rid) - .ok_or_else(bad_resource_id)?; + let resource = state.resource_table.get::(rid)?; if resource.fs_file.is_none() { return Err(not_supported());