Skip to main content

crumbkit_process/
overdue.rs

1//! The report of a run whose child is still running at a chosen time.
2
3use std::fmt;
4use std::sync::Arc;
5use std::time::{Duration, Instant};
6
7/// What a run tells [`RunOptions::overdue`](crate::RunOptions::overdue)'s
8/// report: the child is still running at the overdue time.
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10#[non_exhaustive]
11pub struct Overdue {
12    id: u32,
13    elapsed: Duration,
14}
15
16impl Overdue {
17    pub(crate) const fn new(id: u32, elapsed: Duration) -> Self {
18        Self { id, elapsed }
19    }
20
21    /// Returns the child's process id. The child is unreaped until the run
22    /// ends, so the id names it and no other process.
23    #[must_use]
24    pub const fn id(&self) -> u32 {
25        self.id
26    }
27
28    /// Returns the time from the child's start to the report.
29    #[must_use]
30    pub const fn elapsed(&self) -> Duration {
31        self.elapsed
32    }
33}
34
35/// The caller's report and the time it runs at.
36#[derive(Clone)]
37pub(super) struct Report {
38    pub(super) at: Instant,
39    report: Arc<dyn Fn(Overdue) + Send + Sync>,
40}
41
42impl Report {
43    /// Wraps the caller's function.
44    pub(super) fn new(at: Instant, report: impl Fn(Overdue) + Send + Sync + 'static) -> Self {
45        Self {
46            at,
47            report: Arc::new(report),
48        }
49    }
50
51    /// Calls the caller's function.
52    pub(super) fn call(&self, overdue: Overdue) {
53        (self.report)(overdue);
54    }
55}
56
57impl fmt::Debug for Report {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        f.debug_struct("Report")
60            .field("at", &self.at)
61            .finish_non_exhaustive()
62    }
63}