Skip to content

Commit 5af720d

Browse files
authored
Merge pull request #3953 from Abestanis/freature/instant_try_constructor
Add `Instant::try_from_*` constructor functions
2 parents 0cf9511 + 8697580 commit 5af720d

File tree

2 files changed

+109
-2
lines changed

2 files changed

+109
-2
lines changed

embassy-time/src/duration.rs

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ impl Duration {
6464

6565
/// Creates a duration from the specified number of nanoseconds, rounding up.
6666
/// 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 {
6868
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),
7070
}
7171
}
7272

@@ -90,6 +90,82 @@ impl Duration {
9090
}
9191
}
9292

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+
93169
/// Creates a duration corresponding to the specified Hz.
94170
/// NOTE: Giving this function a hz >= the TICK_HZ of your platform will clamp the Duration to 1
95171
/// tick. Doing so will not deadlock, but will certainly not produce the desired output.

embassy-time/src/instant.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,37 @@ impl Instant {
5050
}
5151
}
5252

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+
5384
/// Tick count since system boot.
5485
pub const fn as_ticks(&self) -> u64 {
5586
self.ticks

0 commit comments

Comments
 (0)