From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 334556AF59 for ; Mon, 25 Jan 2021 14:43:45 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 2AF88ACA8 for ; Mon, 25 Jan 2021 14:43:15 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 9A4E1AC9E for ; Mon, 25 Jan 2021 14:43:14 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 674C445717 for ; Mon, 25 Jan 2021 14:43:14 +0100 (CET) From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= To: pbs-devel@lists.proxmox.com Date: Mon, 25 Jan 2021 14:42:47 +0100 Message-Id: <20210125134302.3394328-3-f.gruenbichler@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210125134302.3394328-1-f.gruenbichler@proxmox.com> References: <20210125134302.3394328-1-f.gruenbichler@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.026 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox-backup 02/15] broadcast_future: refactor broadcast/future binding X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 25 Jan 2021 13:43:45 -0000 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 -- 2.20.1