From 84d07eaa1f9ad5a010a13064e6a9b7fb2a8f5eab Mon Sep 17 00:00:00 2001 From: daxpedda Date: Sun, 24 Dec 2023 20:17:53 +0100 Subject: [PATCH] Add `SystemTimeExt` --- .config/topic.dic | 3 +- CHANGELOG.md | 4 +++ src/lib.rs | 12 ++++++-- src/{web => time}/instant.rs | 0 src/{web => time}/js.rs | 0 src/{web => time}/mod.rs | 0 src/{web => time}/system_time.rs | 2 +- src/web.rs | 49 ++++++++++++++++++++++++++++++++ 8 files changed, 66 insertions(+), 4 deletions(-) rename src/{web => time}/instant.rs (100%) rename src/{web => time}/js.rs (100%) rename src/{web => time}/mod.rs (100%) rename src/{web => time}/system_time.rs (98%) create mode 100644 src/web.rs diff --git a/.config/topic.dic b/.config/topic.dic index b35ca0c..4a27829 100644 --- a/.config/topic.dic +++ b/.config/topic.dic @@ -1,7 +1,8 @@ -16 +17 1G 1M 1ns +APIs Atomics Changelog CHANGELOG diff --git a/CHANGELOG.md b/CHANGELOG.md index dc15e56..1c060e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- Added `web` module containing a platform-specific extension trait to + `SystemTime`, allowing conversion from and to `std::time::SystemTime`. + ### Changed - Improve performance of `SystemTime` by using `Duration` internally. diff --git a/src/lib.rs b/src/lib.rs index 66ee784..dd3bf62 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -111,7 +111,15 @@ target_family = "wasm", not(any(target_os = "emscripten", target_os = "wasi")) ))] -mod web; +mod time; +#[cfg(any( + all( + target_family = "wasm", + not(any(target_os = "emscripten", target_os = "wasi")) + ), + docsrs +))] +pub mod web; #[cfg(not(all( target_family = "wasm", @@ -123,4 +131,4 @@ pub use std::time::*; target_family = "wasm", not(any(target_os = "emscripten", target_os = "wasi")) ))] -pub use self::web::*; +pub use self::time::*; diff --git a/src/web/instant.rs b/src/time/instant.rs similarity index 100% rename from src/web/instant.rs rename to src/time/instant.rs diff --git a/src/web/js.rs b/src/time/js.rs similarity index 100% rename from src/web/js.rs rename to src/time/js.rs diff --git a/src/web/mod.rs b/src/time/mod.rs similarity index 100% rename from src/web/mod.rs rename to src/time/mod.rs diff --git a/src/web/system_time.rs b/src/time/system_time.rs similarity index 98% rename from src/web/system_time.rs rename to src/time/system_time.rs index cee0760..dfc7d11 100644 --- a/src/web/system_time.rs +++ b/src/time/system_time.rs @@ -7,7 +7,7 @@ use std::time::Duration; /// See [`std::time::SystemTime`]. #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] -pub struct SystemTime(Duration); +pub struct SystemTime(pub(crate) Duration); impl SystemTime { /// See [`std::time::SystemTime::UNIX_EPOCH`]. diff --git a/src/web.rs b/src/web.rs new file mode 100644 index 0000000..cf9b66e --- /dev/null +++ b/src/web.rs @@ -0,0 +1,49 @@ +//! Platform-specific extensions to `web-time` for the Web platform. + +#![allow(clippy::absolute_paths)] + +use std::time::SystemTime as StdSystemTime; + +use crate::SystemTime; + +/// Web-specific extension to [`web_time::SystemTime`](crate::SystemTime). +pub trait SystemTimeExt { + /// Convert [`web_time::SystemTime`](crate::SystemTime) to + /// [`std::time::SystemTime`]. + /// + /// # Note + /// + /// This might give a misleading impression of compatibility! + /// + /// Considering this functionality will probably be used to interact with + /// incompatible APIs of other dependencies, care should be taken that the + /// dependency in question doesn't call [`std::time::SystemTime::now()`] + /// internally, which would panic. + fn to_std(self) -> std::time::SystemTime; + + /// Convert [`std::time::SystemTime`] to + /// [`web_time::SystemTime`](crate::SystemTime). + /// + /// # Note + /// + /// This might give a misleading impression of compatibility! + /// + /// Considering this functionality will probably be used to interact with + /// incompatible APIs of other dependencies, care should be taken that the + /// dependency in question doesn't call [`std::time::SystemTime::now()`] + /// internally, which would panic. + fn from_std(time: std::time::SystemTime) -> SystemTime; +} + +impl SystemTimeExt for SystemTime { + fn to_std(self) -> std::time::SystemTime { + StdSystemTime::UNIX_EPOCH + self.0 + } + + fn from_std(time: std::time::SystemTime) -> SystemTime { + Self::UNIX_EPOCH + + time + .duration_since(StdSystemTime::UNIX_EPOCH) + .expect("found `SystemTime` earlier then unix epoch") + } +}