Skip to content

Commit 3c8e981

Browse files
committed
nostr: fix WASM build
1 parent 7d7e7ec commit 3c8e981

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

crates/nostr/src/event/builder.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,7 @@ impl EventBuilder {
206206
where
207207
T: TimeSupplier,
208208
{
209-
#[cfg(target_arch = "wasm32")]
210-
use instant::Instant;
211-
#[cfg(all(not(target_arch = "wasm32"), feature = "std"))]
209+
#[cfg(feature = "std")]
212210
use std::cmp;
213211

214212
#[cfg(all(feature = "alloc", not(feature = "std")))]
@@ -280,9 +278,7 @@ impl EventBuilder {
280278
where
281279
T: TimeSupplier,
282280
{
283-
#[cfg(target_arch = "wasm32")]
284-
use instant::Instant;
285-
#[cfg(all(not(target_arch = "wasm32"), feature = "std"))]
281+
#[cfg(feature = "std")]
286282
use std::cmp;
287283

288284
#[cfg(all(feature = "alloc", not(feature = "std")))]

crates/nostr/src/types/time.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@ use alloc::vec::Vec;
2222
#[cfg(all(feature = "alloc", not(feature = "std")))]
2323
use core::num;
2424

25-
#[cfg(target_arch = "wasm32")]
26-
use instant::SystemTime;
2725
use serde::{Deserialize, Serialize};
2826

29-
#[cfg(target_arch = "wasm32")]
30-
const UNIX_EPOCH: SystemTime = SystemTime::UNIX_EPOCH;
31-
3227
/// Helper trait for acquiring time in `no_std` environments.
3328
pub trait TimeSupplier {
3429
/// The current time from the specified `TimeSupplier`
@@ -61,24 +56,37 @@ impl TimeSupplier for InstantWasm32 {
6156
type Now = InstantWasm32;
6257
type StartingPoint = std::time::SystemTime;
6358

59+
fn now(&self) -> Self::StartingPoint {
60+
SystemTime::now()
61+
}
62+
6463
fn instant_now(&self) -> Self::Now {
6564
InstantWasm32::now()
6665
}
6766

68-
fn starting_point(&self) -> Self::Now {
67+
fn duration_since_starting_point(&self, now: Self::StartingPoint) -> Duration {
68+
now.duration_since(self.starting_point())
69+
.expect("duration_since panicked")
70+
}
71+
72+
fn starting_point(&self) -> Self::StartingPoint {
6973
std::time::UNIX_EPOCH
7074
}
7175

72-
fn elapsed_since(&self, now: Self::Now, since: Self::Now) -> Duration {
76+
fn elapsed_instant_since(&self, now: Self::Now, since: Self::Now) -> Duration {
7377
now - since
7478
}
7579

80+
fn elapsed_since(&self, now: Self::StartingPoint, since: Self::StartingPoint) -> Duration {
81+
now.duration_since(since).expect("duration_since panicked")
82+
}
83+
7684
fn as_i64(&self, duration: Duration) -> i64 {
7785
duration.as_millis() as i64
7886
}
7987

8088
fn to_timestamp(&self, duration: Duration) -> Timestamp {
81-
Timestamp(duration.as_millis() as i64)
89+
Timestamp(self.as_i64(duration))
8290
}
8391
}
8492

0 commit comments

Comments
 (0)