Skip to content

Commit

Permalink
Merge pull request #72 from grafbase/web-time-wasm32-unknown-unknown
Browse files Browse the repository at this point in the history
Enable Web/Wasm compatibility by using `web_time::SystemTime::now()`
  • Loading branch information
dylanhart committed Jan 31, 2024
2 parents 9bc46d0 + 05499bd commit 54bf77e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ uuid = { version = "1.1", optional = true }
postgres-types = { version = "0.2.6", optional = true }
bytes = { version = "1.4.0", optional = true }

[target.wasm32-unknown-unknown.dependencies]
getrandom = { version = "0.2", features = ["js"] }
web-time = "1"

[dev-dependencies]
bencher = "0.1"
serde_derive = "1.0"
Expand Down
6 changes: 2 additions & 4 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ impl Generator {
/// assert!(ulid1 < ulid2);
/// ```
pub fn generate(&mut self) -> Result<Ulid, MonotonicError> {
let now = SystemTime::now();
self.generate_from_datetime(now)
self.generate_from_datetime(crate::time_utils::now())
}

/// Generate a new Ulid matching the given DateTime.
Expand Down Expand Up @@ -91,8 +90,7 @@ impl Generator {
where
R: rand::Rng + ?Sized,
{
let now = SystemTime::now();
self.generate_from_datetime_with_source(now, source)
self.generate_from_datetime_with_source(crate::time_utils::now(), source)
}

/// Generate a new monotonic increasing Ulid with the given source matching the given DateTime
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ mod postgres;
pub mod serde;
#[cfg(feature = "std")]
mod time;
#[cfg(feature = "std")]
mod time_utils;
#[cfg(feature = "uuid")]
mod uuid;

Expand Down
4 changes: 2 additions & 2 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl Ulid {
/// let my_ulid = Ulid::new();
/// ```
pub fn new() -> Ulid {
Ulid::from_datetime(SystemTime::now())
Ulid::from_datetime(crate::time_utils::now())
}

/// Creates a new Ulid using data from the given random number generator
Expand All @@ -25,7 +25,7 @@ impl Ulid {
/// let ulid = Ulid::with_source(&mut rng);
/// ```
pub fn with_source<R: rand::Rng>(source: &mut R) -> Ulid {
Ulid::from_datetime_with_source(SystemTime::now(), source)
Ulid::from_datetime_with_source(crate::time_utils::now(), source)
}

/// Creates a new Ulid with the given datetime
Expand Down
9 changes: 9 additions & 0 deletions src/time_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub(crate) fn now() -> std::time::SystemTime {
#[cfg(target_arch = "wasm32")]
{
use web_time::web::SystemTimeExt;
return web_time::SystemTime::now().to_std();
}
#[cfg(not(target_arch = "wasm32"))]
return std::time::SystemTime::now();
}

0 comments on commit 54bf77e

Please sign in to comment.