aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Scholz <[email protected]>2025-03-07 19:32:42 +0100
committerSebastian Scholz <[email protected]>2025-03-07 19:32:42 +0100
commit2ceb3a721c07ce4154e431f5ca4eb3d2632c95e2 (patch)
treed1b12d0884597e156f96b76b83cad32cd7f093d8
parent7c49f482d71d594d7b48c3393cc98d03a9e7c9e2 (diff)
Add Instant::try_from_* constructor functions
-rw-r--r--embassy-time/src/instant.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/embassy-time/src/instant.rs b/embassy-time/src/instant.rs
index 7fc93c2ec..6571bea62 100644
--- a/embassy-time/src/instant.rs
+++ b/embassy-time/src/instant.rs
@@ -50,6 +50,37 @@ impl Instant {
50 } 50 }
51 } 51 }
52 52
53 /// Try to create an Instant from a microsecond count since system boot.
54 /// Fails if the number of microseconds is too large.
55 pub const fn try_from_micros(micros: u64) -> Option<Self> {
56 let Some(value) = micros.checked_mul(TICK_HZ / GCD_1M) else {
57 return None;
58 };
59 Some(Self {
60 ticks: value / (1_000_000 / GCD_1M),
61 })
62 }
63
64 /// Try to create an Instant from a millisecond count since system boot.
65 /// Fails if the number of milliseconds is too large.
66 pub const fn try_from_millis(millis: u64) -> Option<Self> {
67 let Some(value) = millis.checked_mul(TICK_HZ / GCD_1K) else {
68 return None;
69 };
70 Some(Self {
71 ticks: value / (1000 / GCD_1K),
72 })
73 }
74
75 /// Try to create an Instant from a second count since system boot.
76 /// Fails if the number of seconds is too large.
77 pub const fn try_from_secs(seconds: u64) -> Option<Self> {
78 let Some(ticks) = seconds.checked_mul(TICK_HZ) else {
79 return None;
80 };
81 Some(Self { ticks })
82 }
83
53 /// Tick count since system boot. 84 /// Tick count since system boot.
54 pub const fn as_ticks(&self) -> u64 { 85 pub const fn as_ticks(&self) -> u64 {
55 self.ticks 86 self.ticks