Skip to content

Commit 6facf7c

Browse files
committed
Added docs & made errors public
1 parent 1f77bd0 commit 6facf7c

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

src/i2c.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ mod embedded_hal_impl {
124124
}
125125
}
126126

127+
/// Error type wrapping [LinuxI2CError](i2cdev::linux::LinuxI2CError) to implement [embedded_hal::i2c::ErrorKind]
127128
#[derive(Debug)]
128129
pub struct I2CError {
129130
err: i2cdev::linux::LinuxI2CError,

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ mod spi;
4646
mod timer;
4747

4848
pub use crate::delay::Delay;
49-
pub use crate::i2c::I2cdev;
50-
pub use crate::serial::Serial;
51-
pub use crate::spi::Spidev;
49+
pub use crate::i2c::{I2CError, I2cdev};
50+
pub use crate::serial::{Serial, SerialError};
51+
pub use crate::spi::{SPIError, Spidev};
5252
pub use crate::timer::SysTimer;

src/serial.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ impl Serial {
2020
}
2121

2222
/// Helper to convert std::io::Error to the nb::Error
23-
fn translate_io_errors(err: std::io::Error) -> nb::Error<IoError> {
23+
fn translate_io_errors(err: std::io::Error) -> nb::Error<SerialError> {
2424
match err.kind() {
2525
IoErrorKind::WouldBlock | IoErrorKind::TimedOut | IoErrorKind::Interrupted => {
2626
nb::Error::WouldBlock
2727
}
28-
err => nb::Error::Other(IoError { err }),
28+
err => nb::Error::Other(SerialError { err }),
2929
}
3030
}
3131

3232
impl embedded_hal::serial::nb::Read<u8> for Serial {
33-
type Error = IoError;
33+
type Error = SerialError;
3434

3535
fn read(&mut self) -> nb::Result<u8, Self::Error> {
3636
let mut buffer = [0; 1];
@@ -44,7 +44,7 @@ impl embedded_hal::serial::nb::Read<u8> for Serial {
4444
}
4545

4646
impl embedded_hal::serial::nb::Write<u8> for Serial {
47-
type Error = IoError;
47+
type Error = SerialError;
4848

4949
fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
5050
self.0.write(&[word]).map_err(translate_io_errors)?;
@@ -56,12 +56,13 @@ impl embedded_hal::serial::nb::Write<u8> for Serial {
5656
}
5757
}
5858

59+
/// Error type wrapping [io::ErrorKind](IoErrorKind) to implement [embedded_hal::serial::ErrorKind]
5960
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
60-
pub struct IoError {
61+
pub struct SerialError {
6162
err: IoErrorKind,
6263
}
6364

64-
impl embedded_hal::serial::Error for IoError {
65+
impl embedded_hal::serial::Error for SerialError {
6566
fn kind(&self) -> embedded_hal::serial::ErrorKind {
6667
use embedded_hal::serial::ErrorKind::*;
6768
match &self.err {

src/spi.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,37 +47,37 @@ mod embedded_hal_impl {
4747
use std::io::Write as _;
4848

4949
impl Transfer<u8> for Spidev {
50-
type Error = IoError;
50+
type Error = SPIError;
5151

5252
fn transfer<'b>(&mut self, read: &'b mut [u8], write: &[u8]) -> Result<(), Self::Error> {
5353
self.0
5454
.transfer(&mut SpidevTransfer::read_write(&write, read))
55-
.map_err(|err| IoError { err })
55+
.map_err(|err| SPIError { err })
5656
}
5757
}
5858

5959
impl TransferInplace<u8> for Spidev {
60-
type Error = IoError;
60+
type Error = SPIError;
6161

6262
fn transfer_inplace<'b>(&mut self, buffer: &'b mut [u8]) -> Result<(), Self::Error> {
6363
let tx = buffer.to_owned();
6464
self.0
6565
.transfer(&mut SpidevTransfer::read_write(&tx, buffer))
66-
.map_err(|err| IoError { err })
66+
.map_err(|err| SPIError { err })
6767
}
6868
}
6969

7070
impl Write<u8> for Spidev {
71-
type Error = IoError;
71+
type Error = SPIError;
7272

7373
fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> {
74-
self.0.write_all(buffer).map_err(|err| IoError { err })
74+
self.0.write_all(buffer).map_err(|err| SPIError { err })
7575
}
7676
}
7777

7878
/// Transactional implementation batches SPI operations into a single transaction
7979
impl Transactional<u8> for Spidev {
80-
type Error = IoError;
80+
type Error = SPIError;
8181

8282
fn exec<'a>(&mut self, operations: &mut [SpiOperation<'a, u8>]) -> Result<(), Self::Error> {
8383
// Map types from generic to linux objects
@@ -105,23 +105,24 @@ mod embedded_hal_impl {
105105
// Execute transfer
106106
self.0
107107
.transfer_multiple(&mut messages)
108-
.map_err(|err| IoError { err })
108+
.map_err(|err| SPIError { err })
109109
}
110110
}
111111
}
112112

113+
/// Error type wrapping [io::Error](io::Error) to implement [embedded_hal::spi::ErrorKind]
113114
#[derive(Debug)]
114-
pub struct IoError {
115+
pub struct SPIError {
115116
err: io::Error,
116117
}
117118

118-
impl From<io::Error> for IoError {
119+
impl From<io::Error> for SPIError {
119120
fn from(err: io::Error) -> Self {
120121
Self { err }
121122
}
122123
}
123124

124-
impl embedded_hal::spi::Error for IoError {
125+
impl embedded_hal::spi::Error for SPIError {
125126
fn kind(&self) -> embedded_hal::spi::ErrorKind {
126127
use embedded_hal::spi::ErrorKind::*;
127128
match self.err.kind() {

0 commit comments

Comments
 (0)