Skip to content

Commit d913b84

Browse files
committed
implement core::error::Error
this API has been stabilised with Rust 1.81.0. accordingly the MSRV has been raised. in order to implement `cause` the underlying error from the HAL must also implement `core::error::Error`. otherwise the trait bound will not be satisfied. as the trait has only just been stabilisied it is not yet implemented by any HAL (except for `embedded-hal-mock` since it's `std`) which means that it'll take a moment until this can start to be used.
1 parent 1e4680d commit d913b84

File tree

5 files changed

+62
-6
lines changed

5 files changed

+62
-6
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
rust: [1.63.0, stable]
16+
rust: [1.81.0, stable]
1717
features: ['']
1818
runs-on: ubuntu-latest
1919
steps:

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010

1111
* Add a `current_standby()` method to check if the driver is currently in standby mode.
12+
* `MotorError` and `Tb6612fngError` now implement `core::error::Error` (newly stabilised in Rust 1.81)
1213

1314
### Changed
1415

@@ -20,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2021
* **Breaking**: Renamed error types to their struct names
2122
* **Breaking**: Renamed `DriveCommand::Backwards` to `DriveCommand::Backward` to match
2223
`DriveCommand::Forward`
24+
* The MSRV has been updated to 1.81.0 due to `core::error::Error being implemented`
2325

2426
### Removed
2527

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "tb6612fng"
33
version = "0.2.0"
44
edition = "2021"
5-
rust-version = "1.63"
5+
rust-version = "1.81"
66

77
description = "A `no_std` driver for the TB6612FNG motor driver."
88
repository = "https://github.com/rust-embedded-community/tb6612fng-rs"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ A simple example for the STM32F4 microcontrollers is [available](examples/stm32f
2727
For the changelog please see the dedicated [CHANGELOG.md](CHANGELOG.md).
2828

2929
## Minimum Supported Rust Version (MSRV)
30-
This crate is guaranteed to compile on stable Rust 1.63 and up. It *might*
30+
This crate is guaranteed to compile on stable Rust 1.81 and up. It *might*
3131
compile with older versions but that may change in any new patch release.
3232

3333
## License

src/lib.rs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#![deny(unused)]
1818
#![no_std]
1919

20+
use core::error::Error;
21+
use core::fmt::{Debug, Formatter};
2022
use embedded_hal::digital::{OutputPin, StatefulOutputPin};
2123
use embedded_hal::pwm::SetDutyCycle;
2224

@@ -33,13 +35,62 @@ pub enum MotorError<IN1Error, IN2Error, PWMError> {
3335
PwmError(PWMError),
3436
}
3537

38+
impl<IN1Error: Debug, IN2Error: Debug, PWMError: Debug> core::fmt::Display
39+
for MotorError<IN1Error, IN2Error, PWMError>
40+
{
41+
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
42+
use MotorError::*;
43+
match self {
44+
InvalidSpeed => write!(f, "an invalid speed has been specified"),
45+
In1Error(_) => write!(f, "failed to set the output of the IN1 pin"),
46+
In2Error(_) => write!(f, "failed to set the output of the IN2 pin"),
47+
PwmError(_) => write!(f, "failed to set the output of the PWM pin"),
48+
}
49+
}
50+
}
51+
52+
impl<
53+
IN1Error: Debug + Error + 'static,
54+
IN2Error: Debug + Error + 'static,
55+
PWMError: Debug + Error + 'static,
56+
> Error for MotorError<IN1Error, IN2Error, PWMError>
57+
{
58+
fn source(&self) -> Option<&(dyn Error + 'static)> {
59+
use MotorError::*;
60+
match self {
61+
InvalidSpeed => None,
62+
In1Error(e) => Some(e),
63+
In2Error(e) => Some(e),
64+
PwmError(e) => Some(e),
65+
}
66+
}
67+
}
68+
3669
/// Defines errors which can happen when calling [`Tb6612fng::new()`].
3770
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
3871
pub enum Tb6612fngError<STBYError> {
3972
/// An error in setting the initial output of the standby pin
4073
Standby(STBYError),
4174
}
4275

76+
impl<STBYError: Debug> core::fmt::Display for Tb6612fngError<STBYError> {
77+
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
78+
use Tb6612fngError::*;
79+
match self {
80+
Standby(_) => write!(f, "failed to set the output of the standby pin"),
81+
}
82+
}
83+
}
84+
85+
impl<STBYError: Debug + Error + 'static> Error for Tb6612fngError<STBYError> {
86+
fn source(&self) -> Option<&(dyn Error + 'static)> {
87+
use Tb6612fngError::*;
88+
match self {
89+
Standby(e) => Some(e),
90+
}
91+
}
92+
}
93+
4394
/// Defines the possible drive commands.
4495
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
4596
pub enum DriveCommand {
@@ -89,6 +140,8 @@ where
89140
///
90141
/// # Usage example
91142
/// ```
143+
///
144+
/// # fn main() -> Result<(), Box<dyn core::error::Error>> {
92145
/// # use embedded_hal_mock::eh1::digital::Mock as PinMock;
93146
/// # use embedded_hal_mock::eh1::pwm::Mock as PwmMock;
94147
/// # use embedded_hal_mock::eh1::pwm::Transaction as PwmTransaction;
@@ -113,10 +166,9 @@ where
113166
/// # let mut standby_ = standby.clone();
114167
///
115168
/// use tb6612fng::{Motor, Tb6612fng};
116-
///
117169
/// let controller = Tb6612fng::new(
118-
/// Motor::new(motor_a_in1, motor_a_in2, motor_a_pwm).unwrap(),
119-
/// Motor::new(motor_b_in1, motor_b_in2, motor_b_pwm).unwrap(),
170+
/// Motor::new(motor_a_in1, motor_a_in2, motor_a_pwm)?,
171+
/// Motor::new(motor_b_in1, motor_b_in2, motor_b_pwm)?,
120172
/// standby,
121173
/// );
122174
///
@@ -127,6 +179,8 @@ where
127179
/// # motor_b_in2_.done();
128180
/// # motor_b_pwm_.done();
129181
/// # standby_.done();
182+
/// # Ok(())
183+
/// # }
130184
/// ```
131185
#[allow(clippy::type_complexity)]
132186
pub fn new(

0 commit comments

Comments
 (0)