From 905a570489ddcee05877f3d170cab24b8fe4e2e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= Date: Mon, 25 Jan 2021 14:42:47 +0100 Subject: [PATCH] broadcast_future: refactor broadcast/future binding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit into its own, private struct. Signed-off-by: Fabian Grünbichler --- src/tools/broadcast_future.rs | 42 +++++++++++++++-------------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/src/tools/broadcast_future.rs b/src/tools/broadcast_future.rs index 94aedf18..88b7aaab 100644 --- a/src/tools/broadcast_future.rs +++ b/src/tools/broadcast_future.rs @@ -62,14 +62,16 @@ impl BroadcastData { } } +type SourceFuture = Pin> + Send>>; + +struct BroadCastFutureBinding { + broadcast: BroadcastData, + future: Option>, +} + /// Broadcast future results to registered listeners pub struct BroadcastFuture { - inner: Arc< - Mutex<( - BroadcastData, - Option> + Send>>>, - )>, - >, + inner: Arc>>, } impl BroadcastFuture { @@ -77,7 +79,11 @@ impl BroadcastFuture { /// /// The result of the future is sent to all registered listeners. pub fn new(source: Box> + Send>) -> Self { - Self { inner: Arc::new(Mutex::new((BroadcastData::new(), Some(Pin::from(source))))) } + let inner = BroadCastFutureBinding { + broadcast: BroadcastData::new(), + future: Some(Pin::from(source)), + }; + Self { inner: Arc::new(Mutex::new(inner)) } } /// Creates a new instance with a oneshot channel as trigger @@ -92,29 +98,17 @@ impl BroadcastFuture { } fn notify_listeners( - inner: Arc< - Mutex<( - BroadcastData, - Option> + Send>>>, - )>, - >, + inner: Arc>>, result: Result, ) { let mut data = inner.lock().unwrap(); - data.0.notify_listeners(result); + data.broadcast.notify_listeners(result); } - fn spawn( - inner: Arc< - Mutex<( - BroadcastData, - Option> + Send>>>, - )>, - >, - ) -> impl Future> { + fn spawn(inner: Arc>>) -> impl Future> { let mut data = inner.lock().unwrap(); - if let Some(source) = data.1.take() { + if let Some(source) = data.future.take() { let inner1 = inner.clone(); @@ -127,7 +121,7 @@ impl BroadcastFuture { tokio::spawn(task); } - data.0.listen() + data.broadcast.listen() } /// Register a listener