From 5c1deac0cfe66ef27020aa0e863c16f3bc2afb50 Mon Sep 17 00:00:00 2001 From: Ry Dahl Date: Wed, 6 Nov 2019 12:17:28 -0500 Subject: Remove CoreResource::inspect_repr method (#3274) Towards simplifying (or better removing entirely) the CoreResource trait. Resources should be any bit of privileged heap allocated memory that needs to be referenced from JS, not very specific trait implementations. Therefore CoreResource should be pushed towards being as general as possible. --- core/examples/http_bench.rs | 16 ++++------------ core/resources.rs | 18 ++++++++---------- 2 files changed, 12 insertions(+), 22 deletions(-) (limited to 'core') diff --git a/core/examples/http_bench.rs b/core/examples/http_bench.rs index 764e04303..8635d4f23 100644 --- a/core/examples/http_bench.rs +++ b/core/examples/http_bench.rs @@ -190,19 +190,11 @@ pub fn bad_resource() -> Error { struct TcpListener(tokio::net::TcpListener); -impl Resource for TcpListener { - fn inspect_repr(&self) -> &str { - "tcpListener" - } -} +impl Resource for TcpListener {} struct TcpStream(tokio::net::TcpStream); -impl Resource for TcpStream { - fn inspect_repr(&self) -> &str { - "tcpStream" - } -} +impl Resource for TcpStream {} lazy_static! { static ref RESOURCE_TABLE: Mutex = @@ -225,7 +217,7 @@ fn op_accept(record: Record, _zero_copy_buf: Option) -> Box { .and_then(move |(stream, addr)| { debug!("accept success {}", addr); let mut table = lock_resource_table(); - let rid = table.add(Box::new(TcpStream(stream))); + let rid = table.add("tcpStream", Box::new(TcpStream(stream))); Ok(rid as i32) }); Box::new(fut) @@ -239,7 +231,7 @@ fn op_listen( let addr = "127.0.0.1:4544".parse::().unwrap(); let listener = tokio::net::TcpListener::bind(&addr).unwrap(); let mut table = lock_resource_table(); - let rid = table.add(Box::new(TcpListener(listener))); + let rid = table.add("tcpListener", Box::new(TcpListener(listener))); Box::new(futures::future::ok(rid as i32)) } diff --git a/core/resources.rs b/core/resources.rs index 368f83454..1ba061d0b 100644 --- a/core/resources.rs +++ b/core/resources.rs @@ -17,7 +17,7 @@ pub type ResourceId = u32; /// These store Deno's file descriptors. These are not necessarily the operating /// system ones. -type ResourceMap = HashMap>; +type ResourceMap = HashMap)>; #[derive(Default)] pub struct ResourceTable { @@ -29,7 +29,7 @@ pub struct ResourceTable { impl ResourceTable { pub fn get(&self, rid: ResourceId) -> Option<&T> { - if let Some(resource) = self.map.get(&rid) { + if let Some((_name, resource)) = self.map.get(&rid) { return resource.downcast_ref::(); } @@ -37,7 +37,7 @@ impl ResourceTable { } pub fn get_mut(&mut self, rid: ResourceId) -> Option<&mut T> { - if let Some(resource) = self.map.get_mut(&rid) { + if let Some((_name, resource)) = self.map.get_mut(&rid) { return resource.downcast_mut::(); } @@ -51,9 +51,9 @@ impl ResourceTable { next_rid as ResourceId } - pub fn add(&mut self, resource: Box) -> ResourceId { + pub fn add(&mut self, name: &str, resource: Box) -> ResourceId { let rid = self.next_rid(); - let r = self.map.insert(rid, resource); + let r = self.map.insert(rid, (name.to_string(), resource)); assert!(r.is_none()); rid } @@ -62,18 +62,17 @@ impl ResourceTable { self .map .iter() - .map(|(key, value)| (*key, value.inspect_repr().to_string())) + .map(|(key, (name, _resource))| (*key, name.clone())) .collect() } // close(2) is done by dropping the value. Therefore we just need to remove // the resource from the RESOURCE_TABLE. pub fn close(&mut self, rid: ResourceId) -> Option<()> { - if let Some(resource) = self.map.remove(&rid) { + if let Some((_name, resource)) = self.map.remove(&rid) { resource.close(); return Some(()); } - None } } @@ -81,8 +80,7 @@ impl ResourceTable { /// Abstract type representing resource in Deno. pub trait Resource: Downcast + Any + Send { /// Method that allows to cleanup resource. + // TODO(ry) remove this method. Resources should rely on drop trait instead. fn close(&self) {} - - fn inspect_repr(&self) -> &str; } impl_downcast!(Resource); -- cgit v1.2.3