Skip to content

Commit 8ce43e5

Browse files
committed
Make downcasting safer
1 parent 84dfc74 commit 8ce43e5

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

embedded-hal/src/pwm.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,14 @@ 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!(num <= denom);
9899
let duty = u32::from(num) * u32::from(self.get_max_duty_cycle()) / u32::from(denom);
99-
self.set_duty_cycle(duty as u16)
100+
101+
// This is safe because we know that `num <= denom`, so `duty <= self.get_max_duty_cycle()` (u16)
102+
#[allow(clippy::cast_possible_truncation)]
103+
{
104+
self.set_duty_cycle(duty as u16)
105+
}
100106
}
101107

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

0 commit comments

Comments
 (0)