aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2025-03-11 14:40:58 +0100
committerGitHub <[email protected]>2025-03-11 14:40:58 +0100
commit5af720d9bb74f39d376d4dc22def649146da1d68 (patch)
treef015d3a8b65afe549ee3988cef413ebf4a6af47f
parent0cf951128d8b187821001674cb88e5cbe080a142 (diff)
parent869758037b0cad03d47614d99e8e72340e3aa5f8 (diff)
Merge pull request #3953 from Abestanis/freature/instant_try_constructor
Add `Instant::try_from_*` constructor functions
-rw-r--r--embassy-time/src/duration.rs80
-rw-r--r--embassy-time/src/instant.rs31
2 files changed, 109 insertions, 2 deletions
diff --git a/embassy-time/src/duration.rs b/embassy-time/src/duration.rs
index 647d208e3..dcda705d3 100644
--- a/embassy-time/src/duration.rs
+++ b/embassy-time/src/duration.rs
@@ -64,9 +64,9 @@ impl Duration {
64 64
65 /// Creates a duration from the specified number of nanoseconds, rounding up. 65 /// Creates a duration from the specified number of nanoseconds, rounding up.
66 /// NOTE: Delays this small may be inaccurate. 66 /// NOTE: Delays this small may be inaccurate.
67 pub const fn from_nanos(micros: u64) -> Duration { 67 pub const fn from_nanos(nanoseconds: u64) -> Duration {
68 Duration { 68 Duration {
69 ticks: div_ceil(micros * (TICK_HZ / GCD_1G), 1_000_000_000 / GCD_1G), 69 ticks: div_ceil(nanoseconds * (TICK_HZ / GCD_1G), 1_000_000_000 / GCD_1G),
70 } 70 }
71 } 71 }
72 72
@@ -90,6 +90,82 @@ impl Duration {
90 } 90 }
91 } 91 }
92 92
93 /// Try to create a duration from the specified number of seconds, rounding up.
94 /// Fails if the number of seconds is too large.
95 pub const fn try_from_secs(secs: u64) -> Option<Duration> {
96 let Some(ticks) = secs.checked_mul(TICK_HZ) else {
97 return None;
98 };
99 Some(Duration { ticks })
100 }
101
102 /// Try to create a duration from the specified number of milliseconds, rounding up.
103 /// Fails if the number of milliseconds is too large.
104 pub const fn try_from_millis(millis: u64) -> Option<Duration> {
105 let Some(value) = millis.checked_mul(TICK_HZ / GCD_1K) else {
106 return None;
107 };
108 Some(Duration {
109 ticks: div_ceil(value, 1000 / GCD_1K),
110 })
111 }
112
113 /// Try to create a duration from the specified number of microseconds, rounding up.
114 /// Fails if the number of microseconds is too large.
115 /// NOTE: Delays this small may be inaccurate.
116 pub const fn try_from_micros(micros: u64) -> Option<Duration> {
117 let Some(value) = micros.checked_mul(TICK_HZ / GCD_1M) else {
118 return None;
119 };
120 Some(Duration {
121 ticks: div_ceil(value, 1_000_000 / GCD_1M),
122 })
123 }
124
125 /// Try to create a duration from the specified number of nanoseconds, rounding up.
126 /// Fails if the number of nanoseconds is too large.
127 /// NOTE: Delays this small may be inaccurate.
128 pub const fn try_from_nanos(nanoseconds: u64) -> Option<Duration> {
129 let Some(value) = nanoseconds.checked_mul(TICK_HZ / GCD_1G) else {
130 return None;
131 };
132 Some(Duration {
133 ticks: div_ceil(value, 1_000_000_000 / GCD_1G),
134 })
135 }
136
137 /// Try to create a duration from the specified number of seconds, rounding down.
138 /// Fails if the number of seconds is too large.
139 pub const fn try_from_secs_floor(secs: u64) -> Option<Duration> {
140 let Some(ticks) = secs.checked_mul(TICK_HZ) else {
141 return None;
142 };
143 Some(Duration { ticks })
144 }
145
146 /// Try to create a duration from the specified number of milliseconds, rounding down.
147 /// Fails if the number of milliseconds is too large.
148 pub const fn try_from_millis_floor(millis: u64) -> Option<Duration> {
149 let Some(value) = millis.checked_mul(TICK_HZ / GCD_1K) else {
150 return None;
151 };
152 Some(Duration {
153 ticks: value / (1000 / GCD_1K),
154 })
155 }
156
157 /// Try to create a duration from the specified number of microseconds, rounding down.
158 /// Fails if the number of microseconds is too large.
159 /// NOTE: Delays this small may be inaccurate.
160 pub const fn try_from_micros_floor(micros: u64) -> Option<Duration> {
161 let Some(value) = micros.checked_mul(TICK_HZ / GCD_1M) else {
162 return None;
163 };
164 Some(Duration {
165 ticks: value / (1_000_000 / GCD_1M),
166 })
167 }
168
93 /// Creates a duration corresponding to the specified Hz. 169 /// Creates a duration corresponding to the specified Hz.
94 /// NOTE: Giving this function a hz >= the TICK_HZ of your platform will clamp the Duration to 1 170 /// NOTE: Giving this function a hz >= the TICK_HZ of your platform will clamp the Duration to 1
95 /// tick. Doing so will not deadlock, but will certainly not produce the desired output. 171 /// tick. Doing so will not deadlock, but will certainly not produce the desired output.
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