Skip to content

Commit

Permalink
SERCOM Status/Flags from TryFrom impl to fn
Browse files Browse the repository at this point in the history
The TryFrom approach resulted in warnings, following Rust updates aimed
at stabilizing the never type.

rust-lang/rust#123748
  • Loading branch information
ianrrees committed Aug 19, 2024
1 parent 9916ca5 commit e3a4518
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
15 changes: 9 additions & 6 deletions hal/src/sercom/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,18 @@ bitflags! {
}
}

/// Convert [`Status`] flags into the corresponding [`Error`] variants
impl TryFrom<Status> for () {
type Error = Error;
impl Status {
/// Check whether [`Self`] originates from an error.
///
/// # Errors
///
/// Returns an error if `STATUS` contains `BUFOVF` or `LENERR`
#[inline]
fn try_from(status: Status) -> Result<(), Error> {
pub fn check_bus_error(self) -> Result<(), Error> {
// Buffer overflow has priority
if status.contains(Status::BUFOVF) {
if self.contains(Status::BUFOVF) {
Err(Error::Overflow)
} else if status.contains(Status::LENERR) {
} else if self.contains(Status::LENERR) {
Err(Error::LengthError)
} else {
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion hal/src/sercom/spi/reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ impl<S: Sercom> Registers<S> {
/// Try to read the interrupt flags, but first check the error status flags.
#[inline]
pub fn read_flags_errors(&self) -> Result<Flags, Error> {
self.read_status().try_into()?;
self.read_status().check_bus_error()?;
Ok(self.read_flags())
}
}
2 changes: 1 addition & 1 deletion hal/src/sercom/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ where
/// containing the corresponding [`Flags`] or [`Error`]
#[inline]
fn read_flags_errors(&self) -> Result<Flags, Error> {
self.read_status().try_into()?;
self.read_status().check_bus_error()?;
Ok(self.read_flags())
}

Expand Down
27 changes: 18 additions & 9 deletions hal/src/sercom/uart/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,30 @@ pub enum Error {
CollisionDetected,
}

impl TryFrom<Status> for () {
type Error = Error;

impl Status {
/// Check whether [`Self`] originates from an error.
///
/// # Errors
///
/// Returns an error if `STATUS` contains:
///
/// * `PERR` - Parity Error
/// * `FERR` - Frame Error
/// * `BUFOVF` - Buffer Overflow
/// * `ISF` - Inconsistent Sync Field
/// * `COLL` - Collision Detected
#[inline]
fn try_from(errors: Status) -> Result<(), Error> {
pub fn check_bus_error(self) -> Result<(), Error> {
use Error::*;
if errors.contains(Status::PERR) {
if self.contains(Status::PERR) {
Err(ParityError)
} else if errors.contains(Status::FERR) {
} else if self.contains(Status::FERR) {
Err(FrameError)
} else if errors.contains(Status::BUFOVF) {
} else if self.contains(Status::BUFOVF) {
Err(Overflow)
} else if errors.contains(Status::ISF) {
} else if self.contains(Status::ISF) {
Err(InconsistentSyncField)
} else if errors.contains(Status::COLL) {
} else if self.contains(Status::COLL) {
Err(CollisionDetected)
} else {
Ok(())
Expand Down

0 comments on commit e3a4518

Please sign in to comment.