From b200e6fc3e591f67646832adb9bbf129ee2b2761 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Thu, 3 Dec 2020 23:52:55 +0100 Subject: core: add plumbing for canceling ops when closing a resource (#8661) --- core/resources2.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'core/resources2.rs') diff --git a/core/resources2.rs b/core/resources2.rs index 62cb3f056..92548a556 100644 --- a/core/resources2.rs +++ b/core/resources2.rs @@ -24,6 +24,11 @@ pub trait Resource: Any + 'static { fn name(&self) -> Cow { type_name::().into() } + + /// Resources may implement the `close()` trait method if they need to do + /// resource specific clean-ups, such as cancelling pending futures, after a + /// resource has been removed from the resource table. + fn close(self: Rc) {} } impl dyn Resource { @@ -117,7 +122,7 @@ impl ResourceTable { /// cause the resource to be dropped. However, since resources are reference /// counted, therefore pending ops are not automatically cancelled. pub fn close(&mut self, rid: ResourceId) -> Option<()> { - self.index.remove(&rid).map(|_| ()) + self.index.remove(&rid).map(|resource| resource.close()) } /// Returns an iterator that yields a `(id, name)` pair for every resource -- cgit v1.2.3