Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit f7f03c0

Browse files
author
Hendrik van Antwerpen
committed
Add CancelAfterDuration
1 parent 9bfef2c commit f7f03c0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

stack-graphs/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
//! importantly, each “chunk” of the overall graph only depends on “local” information from the
5656
//! original source file. (a.k.a., it’s incremental!)
5757
58+
use std::time::{Duration, Instant};
59+
5860
use thiserror::Error;
5961

6062
pub mod arena;
@@ -85,6 +87,29 @@ impl CancellationFlag for NoCancellation {
8587
}
8688
}
8789

90+
pub struct CancelAfterDuration {
91+
limit: Duration,
92+
start: Instant,
93+
}
94+
95+
impl CancelAfterDuration {
96+
pub fn new(limit: Duration) -> Self {
97+
Self {
98+
limit,
99+
start: Instant::now(),
100+
}
101+
}
102+
}
103+
104+
impl CancellationFlag for CancelAfterDuration {
105+
fn check(&self, at: &'static str) -> Result<(), CancellationError> {
106+
if self.start.elapsed() > self.limit {
107+
return Err(CancellationError(at));
108+
}
109+
Ok(())
110+
}
111+
}
112+
88113
#[derive(Clone, Debug, Error)]
89114
#[error("Cancelled at \"{0}\"")]
90115
pub struct CancellationError(pub &'static str);

0 commit comments

Comments
 (0)