Skip to content

Commit

Permalink
Merge pull request #531 from nyurik/cast
Browse files Browse the repository at this point in the history
Make downcasting safer
  • Loading branch information
Dirbaio authored Nov 28, 2023
2 parents c10ddae + 6eeb8c4 commit d9e2ca0
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion embedded-hal/src/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,15 @@ pub trait SetDutyCycle: ErrorType {
/// and that `denom` is not zero.
#[inline]
fn set_duty_cycle_fraction(&mut self, num: u16, denom: u16) -> Result<(), Self::Error> {
debug_assert!(denom != 0);
debug_assert!(num <= denom);
let duty = u32::from(num) * u32::from(self.get_max_duty_cycle()) / u32::from(denom);
self.set_duty_cycle(duty as u16)

// This is safe because we know that `num <= denom`, so `duty <= self.get_max_duty_cycle()` (u16)
#[allow(clippy::cast_possible_truncation)]
{
self.set_duty_cycle(duty as u16)
}
}

/// Set the duty cycle to `percent / 100`
Expand Down

0 comments on commit d9e2ca0

Please sign in to comment.