lexe-common
Advanced tools
| { | ||
| "git": { | ||
| "sha1": "8257a3fd75b197c764711e23a598180d44267965" | ||
| "sha1": "791c6621a4c0b7d298a3d436f7c5dc3856a0157f" | ||
| }, | ||
| "path_in_vcs": "public/lexe-common" | ||
| } |
+15
-15
@@ -505,5 +505,5 @@ # This file is automatically @generated by Cargo. | ||
| name = "lexe-byte-array" | ||
| version = "0.1.13" | ||
| version = "0.1.14" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "b2b842421c8ef4268b443efcc57ece972442718bc4aaabae96abf95e2d7965e6" | ||
| checksum = "b64a4e8f169e0f27bd6fff9bc7ff3a8d794b0de7761d9311b762140135eae05a" | ||
| dependencies = [ | ||
@@ -515,3 +515,3 @@ "lexe-hex", | ||
| name = "lexe-common" | ||
| version = "0.1.13" | ||
| version = "0.1.14" | ||
| dependencies = [ | ||
@@ -564,5 +564,5 @@ "anyhow", | ||
| name = "lexe-crypto" | ||
| version = "0.1.13" | ||
| version = "0.1.14" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "98a2bed86a839e475ba03e08f81dd36e3f84dc12b60fcd2fa3538efc56da6fb6" | ||
| checksum = "c3d96523643b5df1bb6e884c92c0ac11998c3d5176fc91d34f376f49c0d4cef2" | ||
| dependencies = [ | ||
@@ -585,5 +585,5 @@ "bcs", | ||
| name = "lexe-enclave" | ||
| version = "0.1.13" | ||
| version = "0.1.14" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "fa3c5489241aec32abbb0ba0b55c3bcd10a08f8b32ff69d98d07911071a30454" | ||
| checksum = "13af5070b2901c87ed67860131d072ad05575bfcbc2c5734e4cdc08857852e5c" | ||
| dependencies = [ | ||
@@ -605,11 +605,11 @@ "cfg-if", | ||
| name = "lexe-hex" | ||
| version = "0.1.13" | ||
| version = "0.1.14" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "45a167787eec98ef98741c0d2734e0f342a59cc2e9934f6d0066b518ed8d3df9" | ||
| checksum = "b9c36b98d141a6a63e829a48025862521b8d4eb626aec81f87a72f493eb69ecf" | ||
| [[package]] | ||
| name = "lexe-serde" | ||
| version = "0.1.13" | ||
| version = "0.1.14" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "6f90739d67b25de09b6904c8c6df7547e35c064004d4db8b6c52de73018cc465" | ||
| checksum = "a9a7648292ed2d442b977d7fd4f5876dadbda3614e3548d8186aea0c00c24145" | ||
| dependencies = [ | ||
@@ -623,5 +623,5 @@ "base64", | ||
| name = "lexe-sha256" | ||
| version = "0.1.13" | ||
| version = "0.1.14" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "f2206969b749b291f666508db766aca76de8aa15bd441b01cdb02ceb6680d129" | ||
| checksum = "15b1d489cffd54dc4847f0104278113c3d0845c341b70c09c015ee15e22131ee" | ||
| dependencies = [ | ||
@@ -636,5 +636,5 @@ "lexe-byte-array", | ||
| name = "lexe-std" | ||
| version = "0.1.13" | ||
| version = "0.1.14" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "add6181c2c9187076252201f4f0df96af82604869e1a36bbd35ee6a375c341af" | ||
| checksum = "54b107e3c0ce50c9a47ecfa5a5f144f7f5b1d52005ba3200806f40a6f592c786" | ||
| dependencies = [ | ||
@@ -641,0 +641,0 @@ "ref-cast", |
+10
-10
@@ -16,3 +16,3 @@ # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO | ||
| name = "lexe-common" | ||
| version = "0.1.13" | ||
| version = "0.1.14" | ||
| authors = [ | ||
@@ -105,21 +105,21 @@ "Max Fang <max@lexe.app>", | ||
| [dependencies.lexe-byte-array] | ||
| version = "0.1.13" | ||
| version = "0.1.14" | ||
| [dependencies.lexe-crypto] | ||
| version = "0.1.13" | ||
| version = "0.1.14" | ||
| [dependencies.lexe-enclave] | ||
| version = "0.1.13" | ||
| version = "0.1.14" | ||
| [dependencies.lexe-hex] | ||
| version = "0.1.13" | ||
| version = "0.1.14" | ||
| [dependencies.lexe-serde] | ||
| version = "0.1.13" | ||
| version = "0.1.14" | ||
| [dependencies.lexe-sha256] | ||
| version = "0.1.13" | ||
| version = "0.1.14" | ||
| [dependencies.lexe-std] | ||
| version = "0.1.13" | ||
| version = "0.1.14" | ||
| features = ["ref-cast"] | ||
@@ -213,7 +213,7 @@ | ||
| [dev-dependencies.lexe-crypto] | ||
| version = "0.1.13" | ||
| version = "0.1.14" | ||
| features = ["test-utils"] | ||
| [dev-dependencies.lexe-enclave] | ||
| version = "0.1.13" | ||
| version = "0.1.14" | ||
| features = ["test-utils"] | ||
@@ -220,0 +220,0 @@ |
+62
-3
@@ -155,8 +155,30 @@ use std::{ | ||
| /// Returns the absolute difference two timestamps as a [`Duration`]. | ||
| /// Returns the [`Duration`] elapsed from `earlier` to `self`, | ||
| /// or [`None`] if `earlier` is later than `self`. | ||
| pub fn checked_duration_since(self, earlier: Self) -> Option<Duration> { | ||
| let dur_ms = self.to_u64().checked_sub(earlier.to_u64())?; | ||
| Some(Duration::from_millis(dur_ms)) | ||
| } | ||
| /// Returns the [`Duration`] elapsed from `earlier` to `self`, | ||
| /// saturating to [`Duration::ZERO`] if `earlier` is later than `self`. | ||
| pub fn saturating_duration_since(self, earlier: Self) -> Duration { | ||
| let dur_ms = self.to_u64().saturating_sub(earlier.to_u64()); | ||
| Duration::from_millis(dur_ms) | ||
| } | ||
| /// Returns the [`Duration`] elapsed since this timestamp, | ||
| /// or [`None`] if it is in the future. | ||
| #[inline] | ||
| pub fn absolute_diff(self, other: Self) -> Duration { | ||
| Duration::from_millis(self.0.abs_diff(other.0)) | ||
| pub fn checked_elapsed(self) -> Option<Duration> { | ||
| Self::now().checked_duration_since(self) | ||
| } | ||
| /// Returns the [`Duration`] elapsed since this timestamp, | ||
| /// saturating to [`Duration::ZERO`] if it is in the future. | ||
| #[inline] | ||
| pub fn saturating_elapsed(self) -> Duration { | ||
| Self::now().saturating_duration_since(self) | ||
| } | ||
| /// Floors the timestamp to the most recent second. | ||
@@ -353,2 +375,39 @@ #[cfg(test)] | ||
| } | ||
| #[test] | ||
| fn timestamp_duration_since() { | ||
| proptest!(|(ts1: TimestampMs, ts2: TimestampMs)| { | ||
| let (lesser, greater) = | ||
| if ts1 <= ts2 { (ts1, ts2) } else { (ts2, ts1) }; | ||
| let diff = | ||
| Duration::from_millis(greater.to_millis() - lesser.to_millis()); | ||
| // `greater` since `lesser` is exactly `diff`. | ||
| prop_assert_eq!(greater.checked_duration_since(lesser), Some(diff)); | ||
| prop_assert_eq!(greater.saturating_duration_since(lesser), diff); | ||
| // `lesser` since `greater` underflows: `None` / saturates to | ||
| // `ZERO`, except when equal, where the diff is just `ZERO`. | ||
| let expected_checked = (lesser == greater).then_some(Duration::ZERO); | ||
| prop_assert_eq!( | ||
| lesser.checked_duration_since(greater), | ||
| expected_checked | ||
| ); | ||
| prop_assert_eq!( | ||
| lesser.saturating_duration_since(greater), | ||
| Duration::ZERO | ||
| ); | ||
| }) | ||
| } | ||
| #[test] | ||
| fn timestamp_elapsed() { | ||
| // `now` lies between MIN and MAX, so a min timestamp has elapsed while | ||
| // a max (far-future) timestamp underflows. | ||
| assert!(TimestampMs::MIN.checked_elapsed().is_some()); | ||
| assert!(TimestampMs::MIN.saturating_elapsed() > Duration::ZERO); | ||
| assert_eq!(TimestampMs::MAX.checked_elapsed(), None); | ||
| assert_eq!(TimestampMs::MAX.saturating_elapsed(), Duration::ZERO); | ||
| } | ||
| } |
Sorry, the diff of this file is not supported yet