qubit_progress/running/running_progress_notifier.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10use std::sync::mpsc::Sender;
11
12use super::running_progress_signal::RunningProgressSignal;
13
14/// Notifies a [`RunningProgressLoop`](crate::RunningProgressLoop) about progress
15/// points and completion.
16///
17/// The notifier is cloneable so workers can share it cheaply. Sending a signal
18/// returns `false` when the loop has already stopped or its receiver was
19/// dropped.
20///
21/// # Examples
22///
23/// ```
24/// use qubit_progress::RunningProgressLoop;
25///
26/// let (_progress_loop, notifier) = RunningProgressLoop::channel();
27///
28/// assert!(notifier.running_point());
29/// assert!(notifier.stop());
30/// ```
31///
32/// # Author
33///
34/// Haixing Hu
35#[derive(Clone)]
36pub struct RunningProgressNotifier {
37 /// Signal sender shared by callers and workers.
38 pub(crate) signal_sender: Sender<RunningProgressSignal>,
39}
40
41impl RunningProgressNotifier {
42 /// Sends a running progress point signal.
43 ///
44 /// # Returns
45 ///
46 /// `true` when the signal was sent, or `false` when the matching loop has
47 /// already stopped.
48 #[inline]
49 pub fn running_point(&self) -> bool {
50 self.signal_sender
51 .send(RunningProgressSignal::RunningPoint)
52 .is_ok()
53 }
54
55 /// Sends a stop signal.
56 ///
57 /// # Returns
58 ///
59 /// `true` when the signal was sent, or `false` when the matching loop has
60 /// already stopped.
61 #[inline]
62 pub fn stop(&self) -> bool {
63 self.signal_sender.send(RunningProgressSignal::Stop).is_ok()
64 }
65}