diff options
author | Ry Dahl <ry@tinyclouds.org> | 2019-11-06 12:17:28 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-06 12:17:28 -0500 |
commit | 5c1deac0cfe66ef27020aa0e863c16f3bc2afb50 (patch) | |
tree | d3de45e89924de58a5ae573dd5086af04e7a19c6 /core/resources.rs | |
parent | 92b8674162aff30a9552b1a07855b685d305830a (diff) |
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.
Diffstat (limited to 'core/resources.rs')
-rw-r--r-- | core/resources.rs | 18 |
1 files changed, 8 insertions, 10 deletions
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<ResourceId, Box<dyn Resource>>; +type ResourceMap = HashMap<ResourceId, (String, Box<dyn Resource>)>; #[derive(Default)] pub struct ResourceTable { @@ -29,7 +29,7 @@ pub struct ResourceTable { impl ResourceTable { pub fn get<T: Resource>(&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::<T>(); } @@ -37,7 +37,7 @@ impl ResourceTable { } pub fn get_mut<T: Resource>(&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::<T>(); } @@ -51,9 +51,9 @@ impl ResourceTable { next_rid as ResourceId } - pub fn add(&mut self, resource: Box<dyn Resource>) -> ResourceId { + pub fn add(&mut self, name: &str, resource: Box<dyn Resource>) -> 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); |