qubit_progress/running/running_progress_points.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 super::running_progress_notifier::RunningProgressNotifier;
11
12/// Worker-side handle for reporting running progress points.
13///
14/// `RunningProgressPoints` deliberately cannot stop or join the reporter
15/// thread. It only wakes the reporter loop for zero-interval progress. For
16/// positive intervals, [`Self::running_point`] is a no-op because the reporter
17/// loop wakes itself on timeout.
18///
19/// # Examples
20///
21/// ```
22/// use std::{
23/// thread,
24/// time::Duration,
25/// };
26///
27/// use qubit_progress::{
28/// NoOpProgressReporter,
29/// Progress,
30/// ProgressCounters,
31/// RunningProgressLoop,
32/// };
33///
34/// let reporter = NoOpProgressReporter;
35///
36/// thread::scope(|scope| {
37/// let progress = Progress::new(&reporter, Duration::ZERO);
38/// let running_progress =
39/// RunningProgressLoop::spawn_scoped(scope, progress, || {
40/// ProgressCounters::new(Some(1)).with_completed_count(1)
41/// });
42/// let progress_points = running_progress.points();
43///
44/// assert!(progress_points.running_point());
45///
46/// running_progress.stop_and_join();
47/// });
48/// ```
49///
50/// # Author
51///
52/// Haixing Hu
53#[derive(Clone)]
54pub struct RunningProgressPoints {
55 /// Optional notifier used only when worker points should wake the loop.
56 notifier: Option<RunningProgressNotifier>,
57}
58
59impl RunningProgressPoints {
60 /// Creates a worker-side running point handle.
61 ///
62 /// # Parameters
63 ///
64 /// * `notifier` - Optional notifier used for zero-interval point signals.
65 ///
66 /// # Returns
67 ///
68 /// A worker-side handle that reports points or no-ops by interval policy.
69 #[inline]
70 pub(crate) const fn new(notifier: Option<RunningProgressNotifier>) -> Self {
71 Self { notifier }
72 }
73
74 /// Reports one worker running progress point.
75 ///
76 /// # Returns
77 ///
78 /// `true` when the point was accepted or no point signal is required.
79 /// Returns `false` only when a required zero-interval signal could not be
80 /// sent because the reporter loop has already stopped.
81 #[inline]
82 pub fn running_point(&self) -> bool {
83 match self.notifier.as_ref() {
84 Some(notifier) => notifier.running_point(),
85 None => true,
86 }
87 }
88}