pub struct Deadline { /* private fields */ }Expand description
When work must stop. Cloning shares the flag: a host keeps one clone to
raise and hands the other to Limits.
use pdfrum_common::Deadline;
// The mechanism: a flag raised from anywhere, on every target.
let stop = Deadline::manual();
let limit = stop.clone();
assert!(!limit.passed());
stop.stop();
assert!(limit.passed());use std::time::Duration;
use pdfrum_common::Deadline;
// The convenience, where there is a clock: a budget from now.
let deadline = Deadline::after(Duration::from_secs(5));
assert!(!deadline.passed());
assert_eq!(deadline.budget(), Some(Duration::from_secs(5)));
// A zero budget has passed before it is asked.
assert!(Deadline::after(Duration::ZERO).passed());Implementations§
Source§impl Deadline
impl Deadline
Sourcepub fn manual() -> Deadline
pub fn manual() -> Deadline
A deadline nothing but Deadline::stop raises.
Sourcepub fn after(budget: Duration) -> Deadline
Available on non-WebAssembly only.
pub fn after(budget: Duration) -> Deadline
A deadline budget from now. It can still be raised early with
Deadline::stop.
Not available on wasm32, which has no monotonic clock to read: a
host there uses Deadline::manual and its own timer.
Sourcepub fn with_budget(&self, budget: Duration) -> Deadline
Available on non-WebAssembly only.
pub fn with_budget(&self, budget: Duration) -> Deadline
This deadline’s flag, with a budget of budget from now.
The returned deadline shares the flag, so Deadline::stop on
either raises both — it is the same deadline with a clock added, not a
second one beside it. That is what a host wants when it holds a cancel
flag and a wall-clock budget: one deadline that answers to whichever
arrives first. Building the two separately gives the engine only one of
them, because Limits::deadline is a single
slot.
An existing budget is replaced, not intersected.
Not available on wasm32, as Deadline::after.
use std::time::Duration;
use pdfrum_common::Deadline;
let flag = Deadline::manual();
let limit = flag.with_budget(Duration::from_secs(30));
// The budget is live on the copy the engine holds...
assert_eq!(limit.budget(), Some(Duration::from_secs(30)));
assert!(!limit.passed());
// ...and the flag still reaches it.
flag.stop();
assert!(limit.passed());Sourcepub fn at(instant: Instant) -> Deadline
Available on non-WebAssembly only.
pub fn at(instant: Instant) -> Deadline
The deadline at instant; one already in the past has passed.
Not available on wasm32, as Deadline::after.
Sourcepub fn stop(&self)
pub fn stop(&self)
Raises the flag: every check from now on, on every clone, answers that the deadline has passed. Idempotent, and callable from any thread.
Sourcepub fn budget(&self) -> Option<Duration>
pub fn budget(&self) -> Option<Duration>
How much time was allowed, for a deadline made with a budget.
Sourcepub fn passed(&self) -> bool
pub fn passed(&self) -> bool
Whether the flag is raised or the budget spent. One atomic load, and a clock read only while the flag is down and a budget is set.
Sourcepub fn check(&self, during: Operation) -> Result<(), LimitExceeded>
pub fn check(&self, during: Operation) -> Result<(), LimitExceeded>
Ok while the budget lasts and the flag is down; past either, the
error that names what was being done. The page, when there is one,
is the caller’s to add with LimitExceeded::on_page.
§Errors
LimitExceeded::Stopped once Deadline::stop was called,
LimitExceeded::Time once a budget is spent.