Skip to main content

web_time_compat/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3// @@ begin lint list maintained by maint/add_warning @@
4#![allow(renamed_and_removed_lints)] // @@REMOVE_WHEN(ci_arti_stable)
5#![allow(unknown_lints)] // @@REMOVE_WHEN(ci_arti_nightly)
6#![warn(missing_docs)]
7#![warn(noop_method_call)]
8#![warn(unreachable_pub)]
9#![warn(clippy::all)]
10#![deny(clippy::await_holding_lock)]
11#![deny(clippy::cargo_common_metadata)]
12#![deny(clippy::cast_lossless)]
13#![deny(clippy::checked_conversions)]
14#![warn(clippy::cognitive_complexity)]
15#![deny(clippy::debug_assert_with_mut_call)]
16#![deny(clippy::exhaustive_enums)]
17#![deny(clippy::exhaustive_structs)]
18#![deny(clippy::expl_impl_clone_on_copy)]
19#![deny(clippy::fallible_impl_from)]
20#![deny(clippy::implicit_clone)]
21#![deny(clippy::large_stack_arrays)]
22#![warn(clippy::manual_ok_or)]
23#![deny(clippy::missing_docs_in_private_items)]
24#![warn(clippy::needless_borrow)]
25#![warn(clippy::needless_pass_by_value)]
26#![warn(clippy::option_option)]
27#![deny(clippy::print_stderr)]
28#![deny(clippy::print_stdout)]
29#![warn(clippy::rc_buffer)]
30#![deny(clippy::ref_option_ref)]
31#![warn(clippy::semicolon_if_nothing_returned)]
32#![warn(clippy::trait_duplication_in_bounds)]
33#![deny(clippy::unchecked_time_subtraction)]
34#![deny(clippy::unnecessary_wraps)]
35#![warn(clippy::unseparated_literal_suffix)]
36#![deny(clippy::unwrap_used)]
37#![deny(clippy::mod_module_files)]
38#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
39#![allow(clippy::uninlined_format_args)]
40#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
41#![allow(clippy::result_large_err)] // temporary workaround for arti#587
42#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
43#![allow(clippy::needless_lifetimes)] // See arti#1765
44#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
45#![allow(clippy::collapsible_if)] // See arti#2342
46#![deny(clippy::unused_async)]
47//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
48
49// We always use `SystemTime` for our data representation outside of this crate.
50//
51// The only time that we touch `SystemTime` is when we are constructing it with
52// `SystemTimeExt::get`.
53pub use std::time::SystemTime;
54
55// "Duration" is the same type in web_time as it is in stdlib.
56pub use std::time::Duration;
57
58#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
59mod stdlib;
60
61#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
62pub use stdlib::*;
63
64#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
65mod wasm;
66
67#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
68pub use wasm::*;
69
70/// Module to hide "Sealed"
71mod seal {
72    /// Trait used to prevent implementing InstantExt or SystemTimeExt outside of this crate.
73    #[allow(unreachable_pub)]
74    pub trait Sealed {}
75}
76
77/// Extension trait for [`std::time::SystemTime`]
78///
79/// This trait adds a `get` method which works like `now`,
80/// but also supports `wasm32-unknown-unknown` environments.
81pub trait SystemTimeExt: seal::Sealed {
82    /// Return the current time.
83    fn get() -> std::time::SystemTime;
84}
85
86/// Extension trait for [`Instant`].
87///
88/// This trait adds a `get` method which works like `now`,
89/// so we can make sure we aren't calling [`std::time::Instant::now`]
90/// on`wasm32-unknown-unknown` environments.
91///
92/// ## Design note
93///
94/// Since we already replace the `std::time::Instant` type with
95/// `web_time::Instant` in this crate, why do we also provide
96/// an extension trait to rename its "now" method?
97///
98/// We do so for two reasons:
99///
100/// 1. Consistency.  With this approach, you don't have to remember
101///    which type uses `get` and which uses `now`.
102/// 2. Enforcement.  This approach makes it possible to use Clippy
103///    to disallow `std::time::Instant::now()` unconditionally,
104///    to make sure that you don't forget to use
105///    the appropriate `web_time_compat::Instant` type instead.
106pub trait InstantExt: seal::Sealed {
107    /// Return the current time.
108    fn get() -> crate::Instant;
109}
110
111impl seal::Sealed for std::time::SystemTime {}
112impl seal::Sealed for crate::Instant {}