Skip to content

Commit d9e2ca0

Browse files
authored
Merge pull request #531 from nyurik/cast
Make downcasting safer
2 parents c10ddae + 6eeb8c4 commit d9e2ca0

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

embedded-hal/src/pwm.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,15 @@ pub trait SetDutyCycle: ErrorType {
9595
/// and that `denom` is not zero.
9696
#[inline]
9797
fn set_duty_cycle_fraction(&mut self, num: u16, denom: u16) -> Result<(), Self::Error> {
98+
debug_assert!(denom != 0);
99+
debug_assert!(num <= denom);
98100
let duty = u32::from(num) * u32::from(self.get_max_duty_cycle()) / u32::from(denom);
99-
self.set_duty_cycle(duty as u16)
101+
102+
// This is safe because we know that `num <= denom`, so `duty <= self.get_max_duty_cycle()` (u16)
103+
#[allow(clippy::cast_possible_truncation)]
104+
{
105+
self.set_duty_cycle(duty as u16)
106+
}
100107
}
101108

102109
/// Set the duty cycle to `percent / 100`

0 commit comments

Comments
 (0)