Next Generation WASM Microkernel Operating System
1// Copyright 2025. Jonas Kruckenberg
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8mod clock;
9mod instant;
10mod sleep;
11#[cfg(test)]
12mod test_util;
13mod timeout;
14mod timer;
15
16use core::{fmt, time::Duration};
17
18pub use clock::{Clock, RawClock, RawClockVTable};
19pub use instant::Instant;
20pub use sleep::{Sleep, sleep, sleep_until};
21pub use timeout::{Timeout, timeout, timeout_at};
22pub use timer::{Deadline, Ticks, Timer};
23
24pub const NANOS_PER_SEC: u64 = 1_000_000_000;
25
26#[derive(Debug, Eq, PartialEq)]
27#[non_exhaustive]
28pub enum TimeError {
29 NoGlobalTimer,
30 DurationTooLong {
31 /// The duration that was requested for a [`Sleep`] or [`Timeout`]
32 /// future.
33 ///
34 /// [`Timeout`]: crate::time::Timeout
35 requested: Duration,
36 /// The [maximum duration][max] supported by this [`Timer`] instance.
37 ///
38 /// [max]: Timer::max_duration
39 max: Duration,
40 },
41}
42
43impl fmt::Display for TimeError {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 match self {
46 TimeError::NoGlobalTimer => f.write_str("no global timer available. Tip: You can configure the global timer with `async_kit::time::set_global_timer`"),
47 TimeError::DurationTooLong { requested, max } => write!(f, "duration too long: {requested:?}. Maximum duration {max:?}"),
48 }
49 }
50}
51
52impl core::error::Error for TimeError {}
53
54#[inline]
55fn max_duration(tick_duration: Duration) -> Duration {
56 tick_duration.saturating_mul(u32::MAX)
57}