1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
#![doc(html_favicon_url = "https://raw.githubusercontent.com/zng-ui/zng/main/examples/image/res/zng-logo-icon.png")]
#![doc(html_logo_url = "https://raw.githubusercontent.com/zng-ui/zng/main/examples/image/res/zng-logo.png")]
//!
//! Configurable instant type and service.
//!
//! # Crate
//!
#![doc = include_str!(concat!("../", std::env!("CARGO_PKG_README")))]
use std::{fmt, ops, time::Duration};
use parking_lot::RwLock;
use zng_app_context::app_local;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
#[cfg(target_arch = "wasm32")]
use web_time::Instant;
/// Instant service.
pub struct INSTANT;
impl INSTANT {
/// Returns an instant corresponding to "now" or an instant configured by the app.
///
/// This method can be called in non-app threads. Apps can override this time in app threads,
/// by default the time is *paused* for each widget OP pass so that all widgets observe the same
/// time on the same pass, you can use [`mode`](Self::mode) to check how `now` updates and you
/// can use the `APP.pause_time_for_update` variable to disable pausing.
pub fn now(&self) -> DInstant {
if zng_app_context::LocalContext::current_app().is_some() {
if let Some(now) = INSTANT_SV.read().now {
return now;
}
}
DInstant(self.epoch().elapsed())
}
/// Instant of first usage of the [`INSTANT`] service in the process, minus one day.
///
/// # Panics
///
/// Panics if called in a non-app thread.
pub fn epoch(&self) -> Instant {
if let Some(t) = *EPOCH.read() {
return t;
}
*EPOCH.write().get_or_insert_with(|| {
let mut now = Instant::now();
// some CI machines (Github Windows) fail to subtract 1 day.
for t in [60 * 60 * 24, 60 * 60, 60 * 30, 60 * 15, 60 * 10, 60] {
if let Some(t) = now.checked_sub(Duration::from_secs(t)) {
now = t;
break;
}
}
now
})
}
/// Defines how the `now` value updates.
///
/// # Panics
///
/// Panics if called in a non-app thread.
pub fn mode(&self) -> InstantMode {
INSTANT_SV.read().mode
}
}
/// App control of the [`INSTANT`] service in an app context.
#[expect(non_camel_case_types)]
pub struct INSTANT_APP;
impl INSTANT_APP {
/// Set how the app controls the time.
///
/// If mode is set to [`InstantMode::Now`] the custom now is unset.
pub fn set_mode(&self, mode: InstantMode) {
let mut sv = INSTANT_SV.write();
sv.mode = mode;
if let InstantMode::Now = mode {
sv.now = None;
}
}
/// Set the [`INSTANT.now`] for the app threads.
///
/// # Panics
///
/// Panics if the mode is [`InstantMode::Now`].
///
/// [`INSTANT.now`]: INSTANT::now
pub fn set_now(&self, now: DInstant) {
let mut sv = INSTANT_SV.write();
if let InstantMode::Now = sv.mode {
panic!("cannot set now with `TimeMode::Now`");
}
sv.now = Some(now);
}
/// Set the [`INSTANT.now`] for the app threads to the current time plus `advance`.
///
/// # Panics
///
/// Panics if the mode is not [`InstantMode::Manual`].
///
/// [`INSTANT.now`]: INSTANT::now
pub fn advance_now(&self, advance: Duration) {
let mut sv = INSTANT_SV.write();
if let InstantMode::Manual = sv.mode {
*sv.now.get_or_insert_with(|| DInstant(INSTANT.epoch().elapsed())) += advance;
} else {
panic!("cannot advance now, not `InstantMode::Manual`");
}
}
/// Unset the custom now value.
pub fn unset_now(&self) {
INSTANT_SV.write().now = None;
}
/// Gets the custom now value.
///
/// This value is returned by [`INSTANT.now`] if set.
///
/// [`INSTANT.now`]: INSTANT::now
pub fn custom_now(&self) -> Option<DInstant> {
INSTANT_SV.read().now
}
/// If mode is [`InstantMode::UpdatePaused`] sets the app custom_now to the current time and returns
/// an object that unsets the custom now on drop.
pub fn pause_for_update(&self) -> Option<InstantUpdatePause> {
let mut sv = INSTANT_SV.write();
match sv.mode {
InstantMode::UpdatePaused => {
let now = DInstant(INSTANT.epoch().elapsed());
sv.now = Some(now);
Some(InstantUpdatePause { now })
}
_ => None,
}
}
}
/// Unset now on drop.
///
/// The time is only unset if it is still set to the same pause time.
#[must_use = "unset_now on drop"]
pub struct InstantUpdatePause {
now: DInstant,
}
impl Drop for InstantUpdatePause {
fn drop(&mut self) {
let mut sv = INSTANT_SV.write();
if sv.now == Some(self.now) {
sv.now = None;
}
}
}
/// Duration elapsed since an epoch.
///
/// By default this is the duration elapsed since the first usage of [`INSTANT`] in the process.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DInstant(Duration);
impl DInstant {
/// Returns the amount of time elapsed since this instant.
pub fn elapsed(self) -> Duration {
INSTANT.now().0 - self.0
}
/// Returns the amount of time elapsed from another instant to this one,
/// or zero duration if that instant is later than this one.
pub fn duration_since(self, earlier: DInstant) -> Duration {
self.0 - earlier.0
}
/// Returns `Some(t)` where t is the time `self + duration` if t can be represented.
pub fn checked_add(&self, duration: Duration) -> Option<DInstant> {
self.0.checked_add(duration).map(Self)
}
/// Returns `Some(t)`` where t is the time `self - duration` if `duration` greater then the elapsed time
/// since the process start.
pub fn checked_sub(self, duration: Duration) -> Option<DInstant> {
self.0.checked_sub(duration).map(Self)
}
/// Returns the amount of time elapsed from another instant to this one, or None if that instant is later than this one.
pub fn checked_duration_since(&self, earlier: DInstant) -> Option<Duration> {
self.0.checked_sub(earlier.0)
}
/// Returns the amount of time elapsed from another instant to this one, or zero duration if that instant is later than this one.
pub fn saturating_duration_since(&self, earlier: DInstant) -> Duration {
self.0.saturating_sub(earlier.0)
}
/// Earliest instant.
pub const EPOCH: DInstant = DInstant(Duration::ZERO);
/// The maximum representable instant.
pub const MAX: DInstant = DInstant(Duration::MAX);
}
impl ops::Add<Duration> for DInstant {
type Output = Self;
fn add(self, rhs: Duration) -> Self {
Self(self.0.saturating_add(rhs))
}
}
impl ops::AddAssign<Duration> for DInstant {
fn add_assign(&mut self, rhs: Duration) {
self.0 = self.0.saturating_add(rhs);
}
}
impl ops::Sub<Duration> for DInstant {
type Output = Self;
fn sub(self, rhs: Duration) -> Self {
Self(self.0.saturating_sub(rhs))
}
}
impl ops::SubAssign<Duration> for DInstant {
fn sub_assign(&mut self, rhs: Duration) {
self.0 = self.0.saturating_sub(rhs);
}
}
impl ops::Sub for DInstant {
type Output = Duration;
fn sub(self, rhs: Self) -> Self::Output {
self.0.saturating_sub(rhs.0)
}
}
impl From<DInstant> for Instant {
fn from(t: DInstant) -> Self {
INSTANT.epoch() + t.0
}
}
/// Defines how the [`INSTANT.now`] value updates in the app.
///
/// [`INSTANT.now`]: INSTANT::now
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum InstantMode {
/// Calls during an update pass (or layout, render, etc.) read the same time.
/// Other calls to `now` resamples the time.
UpdatePaused,
/// Every call to `now` resamples the time.
Now,
/// Time is controlled by the app.
Manual,
}
static EPOCH: RwLock<Option<Instant>> = RwLock::new(None);
app_local! {
static INSTANT_SV: InstantService = const {
InstantService {
mode: InstantMode::UpdatePaused,
now: None,
}
};
}
struct InstantService {
mode: InstantMode,
now: Option<DInstant>,
}
/// Represents a timeout instant.
///
/// Deadlines and timeouts can be specified as a [`DInstant`] in the future or as a [`Duration`] from now, both
/// of these types can be converted to this `struct`.
///
/// # Examples
///
/// In the example below the timer function accepts `Deadline`, `DInstant` and `Duration` inputs.
///
/// ```
/// # use zng_time::*;
/// # trait TimeUnits { fn secs(self) -> std::time::Duration where Self: Sized { std::time::Duration::ZERO } }
/// # impl TimeUnits for i32 { }
/// fn timer(deadline: impl Into<Deadline>) {
/// let deadline = deadline.into();
/// // ..
/// }
///
/// timer(5.secs());
/// ```
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Deadline(pub DInstant);
impl Deadline {
/// New deadline from now + `dur`.
pub fn timeout(dur: Duration) -> Self {
Deadline(INSTANT.now() + dur)
}
/// Returns `true` if the deadline was reached.
pub fn has_elapsed(self) -> bool {
self.0 <= INSTANT.now()
}
/// Returns the time left until the deadline is reached.
pub fn time_left(self) -> Option<Duration> {
self.0.checked_duration_since(INSTANT.now())
}
/// Returns the deadline further into the past or closest to now.
pub fn min(self, other: Deadline) -> Deadline {
Deadline(self.0.min(other.0))
}
/// Returns the deadline further into the future.
pub fn max(self, other: Deadline) -> Deadline {
Deadline(self.0.max(other.0))
}
/// Deadline that is always elapsed.
pub const ELAPSED: Deadline = Deadline(DInstant::EPOCH);
/// Deadline that is practically never reached.
pub const MAX: Deadline = Deadline(DInstant::MAX);
}
impl fmt::Display for Deadline {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let dur = self.0 - INSTANT.now();
write!(f, "{dur:?} left")
}
}
impl fmt::Debug for Deadline {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Deadline({self})")
}
}
impl From<DInstant> for Deadline {
fn from(value: DInstant) -> Self {
Deadline(value)
}
}
impl From<Duration> for Deadline {
fn from(value: Duration) -> Self {
Deadline::timeout(value)
}
}
impl ops::Add<Duration> for Deadline {
type Output = Self;
fn add(mut self, rhs: Duration) -> Self {
self.0 += rhs;
self
}
}
impl ops::AddAssign<Duration> for Deadline {
fn add_assign(&mut self, rhs: Duration) {
self.0 += rhs;
}
}
impl ops::Sub<Duration> for Deadline {
type Output = Self;
fn sub(mut self, rhs: Duration) -> Self {
self.0 -= rhs;
self
}
}
impl ops::SubAssign<Duration> for Deadline {
fn sub_assign(&mut self, rhs: Duration) {
self.0 -= rhs;
}
}