From b22b7972786d952559e620a9451a195fc185bde1 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 23 Jan 2017 20:11:37 -0800 Subject: [PATCH] Add a function to upgrade a Remote to a Handle Intended for fast paths which can be a little more optimized for when they're run directly on the event loop. --- src/reactor/mod.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/reactor/mod.rs b/src/reactor/mod.rs index 4d52ad998..914e3fc37 100644 --- a/src/reactor/mod.rs +++ b/src/reactor/mod.rs @@ -606,6 +606,33 @@ impl Remote { pub fn id(&self) -> CoreId { CoreId(self.id) } + + /// Attempts to "promote" this remote to a handle, if possible. + /// + /// This function is intended for structures which typically work through a + /// `Remote` but want to optimize runtime when the remote doesn't actually + /// leave the thread of the original reactor. This will attempt to return a + /// handle if the `Remote` is on the same thread as the event loop and the + /// event loop is running. + /// + /// If this `Remote` has moved to a different thread or if the event loop is + /// running, then `None` may be returned. If you need to guarantee access to + /// a `Handle`, then you can call this function and fall back to using + /// `spawn` above if it returns `None`. + pub fn handle(&self) -> Option { + if CURRENT_LOOP.is_set() { + CURRENT_LOOP.with(|lp| { + let same = lp.inner.borrow().id == self.id; + if same { + Some(lp.handle()) + } else { + None + } + }) + } else { + None + } + } } impl Handle {