Skip to content

Commit a4fb672

Browse files
committed
Make Pwm trait fallible
1 parent 9189259 commit a4fb672

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

src/lib.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -815,39 +815,44 @@ pub trait Capture {
815815
/// # Pwm1
816816
/// };
817817
///
818-
/// pwm.set_period(1.khz());
818+
/// pwm.try_set_period(1.khz()).unwrap();
819819
///
820-
/// let max_duty = pwm.get_max_duty();
820+
/// let max_duty = pwm.try_get_max_duty().unwrap();
821821
///
822822
/// // brightest LED
823-
/// pwm.set_duty(Channel::_1, max_duty);
823+
/// pwm.try_set_duty(Channel::_1, max_duty).unwrap();
824824
///
825825
/// // dimmer LED
826-
/// pwm.set_duty(Channel::_2, max_duty / 4);
826+
/// pwm.try_set_duty(Channel::_2, max_duty / 4).unwrap();
827827
/// }
828828
///
829+
/// # use core::convert::Infallible;
829830
/// # struct KiloHertz(u32);
830831
/// # trait U32Ext { fn khz(self) -> KiloHertz; }
831832
/// # impl U32Ext for u32 { fn khz(self) -> KiloHertz { KiloHertz(self) } }
832833
/// # enum Channel { _1, _2 }
833834
/// # struct Pwm1;
834835
/// # impl hal::Pwm for Pwm1 {
836+
/// # type Error = Infallible;
835837
/// # type Channel = Channel;
836838
/// # type Time = KiloHertz;
837839
/// # type Duty = u16;
838-
/// # fn disable(&mut self, _: Channel) { unimplemented!() }
839-
/// # fn enable(&mut self, _: Channel) { unimplemented!() }
840-
/// # fn get_duty(&self, _: Channel) -> u16 { unimplemented!() }
841-
/// # fn get_max_duty(&self) -> u16 { 0 }
842-
/// # fn set_duty(&mut self, _: Channel, _: u16) {}
843-
/// # fn get_period(&self) -> KiloHertz { unimplemented!() }
844-
/// # fn set_period<T>(&mut self, _: T) where T: Into<KiloHertz> {}
840+
/// # fn try_disable(&mut self, _: Channel) -> Result<(), Self::Error> { unimplemented!() }
841+
/// # fn try_enable(&mut self, _: Channel) -> Result<(), Self::Error> { unimplemented!() }
842+
/// # fn try_get_duty(&self, _: Channel) -> Result<u16, Self::Error> { unimplemented!() }
843+
/// # fn try_get_max_duty(&self) -> Result<u16, Self::Error> { 0 }
844+
/// # fn try_set_duty(&mut self, _: Channel, _: u16) -> Result<(), Self::Error> {}
845+
/// # fn try_get_period(&self) -> Result<KiloHertz, Self::Error> { unimplemented!() }
846+
/// # fn try_set_period<T>(&mut self, _: T) -> Result<(), Self::Error> where T: Into<KiloHertz> {}
845847
/// # }
846848
/// ```
847849
#[cfg(feature = "unproven")]
848850
// reason: pre-singletons API. The `PwmPin` trait seems more useful because it models independent
849851
// PWM channels. Here a certain number of channels are multiplexed in a single implementer.
850852
pub trait Pwm {
853+
/// Enumeration of `Pwm` errors
854+
type Error;
855+
851856
/// Enumeration of channels that can be used with this `Pwm` interface
852857
///
853858
/// If your `Pwm` interface has no channels you can use the type `()`
@@ -864,25 +869,26 @@ pub trait Pwm {
864869
type Duty;
865870

866871
/// Disables a PWM `channel`
867-
fn disable(&mut self, channel: Self::Channel);
872+
fn try_disable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>;
868873

869874
/// Enables a PWM `channel`
870-
fn enable(&mut self, channel: Self::Channel);
875+
fn try_enable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>;
871876

872877
/// Returns the current PWM period
873-
fn get_period(&self) -> Self::Time;
878+
fn try_get_period(&self) -> Result<Self::Time, Self::Error>;
874879

875880
/// Returns the current duty cycle
876-
fn get_duty(&self, channel: Self::Channel) -> Self::Duty;
881+
fn try_get_duty(&self, channel: Self::Channel) -> Result<Self::Duty, Self::Error>;
877882

878883
/// Returns the maximum duty cycle value
879-
fn get_max_duty(&self) -> Self::Duty;
884+
fn try_get_max_duty(&self) -> Result<Self::Duty, Self::Error>;
880885

881886
/// Sets a new duty cycle
882-
fn set_duty(&mut self, channel: Self::Channel, duty: Self::Duty);
887+
fn try_set_duty(&mut self, channel: Self::Channel, duty: Self::Duty)
888+
-> Result<(), Self::Error>;
883889

884890
/// Sets a new PWM period
885-
fn set_period<P>(&mut self, period: P)
891+
fn try_set_period<P>(&mut self, period: P) -> Result<(), Self::Error>
886892
where
887893
P: Into<Self::Time>;
888894
}

0 commit comments

Comments
 (0)