Skip to content

spi: enforce all traits have the same Error type. #331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed
- Fixed blanket impl of `DelayUs` not covering the `delay_ms` method.
### Changed
- `spi`: traits now enforce all impls on the same struct (eg `Transfer` and `Write`) have the same `Error` type.

## [v1.0.0-alpha.6] - 2021-11-19

Expand Down
44 changes: 8 additions & 36 deletions src/spi/blocking.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//! Blocking SPI API

/// Blocking transfer with separate buffers
pub trait Transfer<W: Copy = u8> {
/// Error type
type Error: crate::spi::Error;
use super::ErrorType;

/// Blocking transfer with separate buffers
pub trait Transfer<W = u8>: ErrorType {
/// Writes and reads simultaneously. `write` is written to the slave on MOSI and
/// words received on MISO are stored in `read`.
///
Expand All @@ -17,37 +16,27 @@ pub trait Transfer<W: Copy = u8> {
}

impl<T: Transfer<W>, W: Copy> Transfer<W> for &mut T {
type Error = T::Error;

fn transfer(&mut self, read: &mut [W], write: &[W]) -> Result<(), Self::Error> {
T::transfer(self, read, write)
}
}

/// Blocking transfer with single buffer (in-place)
pub trait TransferInplace<W: Copy = u8> {
/// Error type
type Error: crate::spi::Error;

pub trait TransferInplace<W: Copy = u8>: ErrorType {
/// Writes and reads simultaneously. The contents of `words` are
/// written to the slave, and the received words are stored into the same
/// `words` buffer, overwriting it.
fn transfer_inplace(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
}

impl<T: TransferInplace<W>, W: Copy> TransferInplace<W> for &mut T {
type Error = T::Error;

fn transfer_inplace(&mut self, words: &mut [W]) -> Result<(), Self::Error> {
T::transfer_inplace(self, words)
}
}

/// Blocking read
pub trait Read<W: Copy = u8> {
/// Error type
type Error: crate::spi::Error;

pub trait Read<W: Copy = u8>: ErrorType {
/// Reads `words` from the slave.
///
/// The word value sent on MOSI during reading is implementation-defined,
Expand All @@ -56,44 +45,32 @@ pub trait Read<W: Copy = u8> {
}

impl<T: Read<W>, W: Copy> Read<W> for &mut T {
type Error = T::Error;

fn read(&mut self, words: &mut [W]) -> Result<(), Self::Error> {
T::read(self, words)
}
}

/// Blocking write
pub trait Write<W: Copy = u8> {
/// Error type
type Error: crate::spi::Error;

pub trait Write<W: Copy = u8>: ErrorType {
/// Writes `words` to the slave, ignoring all the incoming words
fn write(&mut self, words: &[W]) -> Result<(), Self::Error>;
}

impl<T: Write<W>, W: Copy> Write<W> for &mut T {
type Error = T::Error;

fn write(&mut self, words: &[W]) -> Result<(), Self::Error> {
T::write(self, words)
}
}

/// Blocking write (iterator version)
pub trait WriteIter<W: Copy = u8> {
/// Error type
type Error: crate::spi::Error;

pub trait WriteIter<W: Copy = u8>: ErrorType {
/// Writes `words` to the slave, ignoring all the incoming words
fn write_iter<WI>(&mut self, words: WI) -> Result<(), Self::Error>
where
WI: IntoIterator<Item = W>;
}

impl<T: WriteIter<W>, W: Copy> WriteIter<W> for &mut T {
type Error = T::Error;

fn write_iter<WI>(&mut self, words: WI) -> Result<(), Self::Error>
where
WI: IntoIterator<Item = W>,
Expand All @@ -119,17 +96,12 @@ pub enum Operation<'a, W: 'static + Copy = u8> {

/// Transactional trait allows multiple actions to be executed
/// as part of a single SPI transaction
pub trait Transactional<W: 'static + Copy = u8> {
/// Associated error type
type Error: crate::spi::Error;

pub trait Transactional<W: 'static + Copy = u8>: ErrorType {
/// Execute the provided transactions
fn exec<'a>(&mut self, operations: &mut [Operation<'a, W>]) -> Result<(), Self::Error>;
}

impl<T: Transactional<W>, W: 'static + Copy> Transactional<W> for &mut T {
type Error = T::Error;

fn exec<'a>(&mut self, operations: &mut [Operation<'a, W>]) -> Result<(), Self::Error> {
T::exec(self, operations)
}
Expand Down
12 changes: 12 additions & 0 deletions src/spi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,15 @@ impl core::fmt::Display for ErrorKind {
}
}
}

/// SPI error type trait
///
/// This just defines the error type, to be used by the other SPI traits.
pub trait ErrorType {
/// Error type
type Error: Error;
}

impl<T: ErrorType> ErrorType for &mut T {
type Error = T::Error;
}
9 changes: 3 additions & 6 deletions src/spi/nb.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Serial Peripheral Interface

use super::ErrorType;

/// Full duplex (master mode)
///
/// # Notes
Expand All @@ -16,10 +18,7 @@
///
/// - Some SPIs can work with 8-bit *and* 16-bit words. You can overload this trait with different
/// `Word` types to allow operation in both modes.
pub trait FullDuplex<Word: Copy = u8> {
/// An enumeration of SPI errors
type Error: crate::spi::Error;

pub trait FullDuplex<Word: Copy = u8>: ErrorType {
/// Reads the word stored in the shift register
///
/// **NOTE** A word must be sent to the slave before attempting to call this
Expand All @@ -31,8 +30,6 @@ pub trait FullDuplex<Word: Copy = u8> {
}

impl<T: FullDuplex<Word>, Word: Copy> FullDuplex<Word> for &mut T {
type Error = T::Error;

fn read(&mut self) -> nb::Result<Word, Self::Error> {
T::read(self)
}
Expand Down