Struct deno_core::ResourceTable
source · pub struct ResourceTable { /* private fields */ }
Expand description
Map-like data structure storing Deno’s resources (equivalent to file descriptors).
Provides basic methods for element access. A resource can be of any type. Different types of resources can be stored in the same map, and provided with a name for description.
Each resource is identified through a resource ID (rid), which acts as the key in the map.
Implementations§
source§impl ResourceTable
impl ResourceTable
sourcepub fn add<T: Resource>(&mut self, resource: T) -> ResourceId
pub fn add<T: Resource>(&mut self, resource: T) -> ResourceId
Inserts resource into the resource table, which takes ownership of it.
The resource type is erased at runtime and must be statically known
when retrieving it through get()
.
Returns a unique resource ID, which acts as a key for this resource.
sourcepub fn add_rc<T: Resource>(&mut self, resource: Rc<T>) -> ResourceId
pub fn add_rc<T: Resource>(&mut self, resource: Rc<T>) -> ResourceId
Inserts a Rc
-wrapped resource into the resource table.
The resource type is erased at runtime and must be statically known
when retrieving it through get()
.
Returns a unique resource ID, which acts as a key for this resource.
pub fn add_rc_dyn(&mut self, resource: Rc<dyn Resource>) -> ResourceId
sourcepub fn has(&self, rid: ResourceId) -> bool
pub fn has(&self, rid: ResourceId) -> bool
Returns true if any resource with the given rid
exists.
sourcepub fn get<T: Resource>(&self, rid: ResourceId) -> Result<Rc<T>, Error>
pub fn get<T: Resource>(&self, rid: ResourceId) -> Result<Rc<T>, Error>
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_any(&self, rid: ResourceId) -> Result<Rc<dyn Resource>, Error>
sourcepub fn replace<T: Resource>(&mut self, rid: ResourceId, resource: T)
pub fn replace<T: Resource>(&mut self, rid: ResourceId, resource: T)
Replaces a resource with a new resource.
Panics if the resource does not exist.
sourcepub fn take<T: Resource>(&mut self, rid: ResourceId) -> Result<Rc<T>, Error>
pub fn take<T: Resource>(&mut self, rid: ResourceId) -> Result<Rc<T>, Error>
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.
sourcepub fn take_any(&mut self, rid: ResourceId) -> Result<Rc<dyn Resource>, Error>
pub fn take_any(&mut self, rid: ResourceId) -> Result<Rc<dyn Resource>, Error>
Removes a resource from the resource table and returns it. Note that the
resource’s close()
method is not called.
sourcepub fn close(&mut self, rid: ResourceId) -> Result<(), Error>
pub fn close(&mut self, rid: ResourceId) -> Result<(), Error>
Removes the resource with the given rid
from the resource table. If the
only reference to this resource existed in the resource table, this will
cause the resource to be dropped. However, since resources are reference
counted, therefore pending ops are not automatically cancelled. A resource
may implement the close()
method to perform clean-ups such as canceling
ops.
sourcepub fn names(&self) -> impl Iterator<Item = (ResourceId, Cow<'_, str>)>
pub fn names(&self) -> impl Iterator<Item = (ResourceId, Cow<'_, str>)>
Returns an iterator that yields a (id, name)
pair for every resource
that’s currently in the resource table. This can be used for debugging
purposes or to implement the op_resources
op. Note that the order in
which items appear is not specified.
Example
let resource_names = resource_table.names().collect::<Vec<_>>();