Skip to content

Commit b60c39a

Browse files
bors[bot]eldruin
andauthored
Merge #278
278: [Reorganization Proposal 1] Move traits into blocking/nonblocking modules r=therealprof a=eldruin As discussed in our weekly meeting. I chose `nonblocking` as module name because `nb` could be misunderstood as a re-exported `nb` crate. An alternative is #279 Co-authored-by: Diego Barrios Romero <[email protected]>
2 parents e835410 + 4f60777 commit b60c39a

18 files changed

+82
-62
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1414
### Changed
1515
- Swap PWM channel arguments to references
1616
- All trait methods have been renamed to remove the `try_` prefix (i.e. `try_send` -> `send`) for consistency.
17+
- Moved all traits into two modules depending on the execution model: `blocking` and `nb` (non-blocking).
18+
- Re-export `nb::{block!, Error, Result}` to avoid version mismatches. These should be used instead of
19+
importing the `nb` crate directly in dependendent crates.
1720

1821
## [v1.0.0-alpha.4] - 2020-11-11
1922

src/digital.rs renamed to src/blocking/digital.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::{convert::From, ops::Not};
77
/// Conversion from `bool` and logical negation are also implemented
88
/// for this type.
99
/// ```rust
10-
/// # use embedded_hal::digital::PinState;
10+
/// # use embedded_hal::blocking::digital::PinState;
1111
/// let state = PinState::from(false);
1212
/// assert_eq!(state, PinState::Low);
1313
/// assert_eq!(!state, PinState::High);
@@ -100,8 +100,8 @@ pub trait ToggleableOutputPin {
100100
/// toggleable by software.
101101
///
102102
/// ```
103-
/// use embedded_hal::digital::{OutputPin, StatefulOutputPin, ToggleableOutputPin};
104-
/// use embedded_hal::digital::toggleable;
103+
/// use embedded_hal::blocking::digital::{OutputPin, StatefulOutputPin, ToggleableOutputPin};
104+
/// use embedded_hal::blocking::digital::toggleable;
105105
/// use core::convert::Infallible;
106106
///
107107
/// /// A virtual output pin that exists purely in software
@@ -182,7 +182,7 @@ pub trait InputPin {
182182
///
183183
/// ```
184184
/// use core::time::Duration;
185-
/// use embedded_hal::digital::{IoPin, InputPin, OutputPin};
185+
/// use embedded_hal::blocking::digital::{IoPin, InputPin, OutputPin};
186186
///
187187
/// pub fn ping_and_read<TInputPin, TOutputPin, TError>(
188188
/// mut pin: TOutputPin, delay_fn: &dyn Fn(Duration) -> ()) -> Result<bool, TError>

src/blocking/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
//! Implementing that marker trait will opt in your type into a blanket implementation.
66
77
pub mod delay;
8+
pub mod digital;
89
pub mod i2c;
10+
pub mod pwm;
11+
pub mod qei;
912
pub mod rng;
1013
pub mod serial;
1114
pub mod spi;
15+
pub mod watchdog;

src/pwm.rs renamed to src/blocking/pwm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
/// # impl U32Ext for u32 { fn khz(self) -> KiloHertz { KiloHertz(self) } }
3535
/// # enum Channel { _1, _2 }
3636
/// # struct Pwm1;
37-
/// # impl hal::pwm::Pwm for Pwm1 {
37+
/// # impl hal::blocking::pwm::Pwm for Pwm1 {
3838
/// # type Error = Infallible;
3939
/// # type Channel = Channel;
4040
/// # type Time = KiloHertz;

src/qei.rs renamed to src/blocking/qei.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@
3838
/// # trait U32Ext { fn s(self) -> Seconds; }
3939
/// # impl U32Ext for u32 { fn s(self) -> Seconds { Seconds(self) } }
4040
/// # struct Qei1;
41-
/// # impl hal::qei::Qei for Qei1 {
41+
/// # impl hal::blocking::qei::Qei for Qei1 {
4242
/// # type Error = Infallible;
4343
/// # type Count = u16;
4444
/// # fn count(&self) -> Result<u16, Self::Error> { Ok(0) }
45-
/// # fn direction(&self) -> Result<::hal::qei::Direction, Self::Error> { unimplemented!() }
45+
/// # fn direction(&self) -> Result<::hal::blocking::qei::Direction, Self::Error> { unimplemented!() }
4646
/// # }
4747
/// # struct Timer6;
48-
/// # impl hal::timer::CountDown for Timer6 {
48+
/// # impl hal::nb::timer::CountDown for Timer6 {
4949
/// # type Error = Infallible;
5050
/// # type Time = Seconds;
5151
/// # fn start<T>(&mut self, _: T) -> Result<(), Infallible> where T: Into<Seconds> { Ok(()) }

src/blocking/serial.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ pub trait Write<Word> {
2323
pub mod write {
2424
/// Marker trait to opt into default blocking write implementation
2525
///
26-
/// Implementers of [`serial::Write`] can implement this marker trait
26+
/// Implementers of [`nonblocking::serial::Write`] can implement this marker trait
2727
/// for their type. Doing so will automatically provide the default
2828
/// implementation of [`blocking::serial::Write`] for the type.
2929
///
30-
/// [`serial::Write`]: ../../serial/trait.Write.html
30+
/// [`nonblocking::serial::Write`]: ../../nonblocking/serial/trait.Write.html
3131
/// [`blocking::serial::Write`]: ../trait.Write.html
32-
pub trait Default<Word>: crate::serial::Write<Word> {}
32+
pub trait Default<Word>: crate::nb::serial::Write<Word> {}
3333

3434
impl<S, Word> crate::blocking::serial::Write<Word> for S
3535
where

src/blocking/spi.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ pub trait WriteIter<W> {
3232
/// Blocking transfer
3333
pub mod transfer {
3434
/// Default implementation of `blocking::spi::Transfer<W>` for implementers of
35-
/// `spi::FullDuplex<W>`
36-
pub trait Default<W>: crate::spi::FullDuplex<W> {}
35+
/// `nonblocking::spi::FullDuplex<W>`
36+
pub trait Default<W>: crate::nb::spi::FullDuplex<W> {}
3737

3838
impl<W, S> crate::blocking::spi::Transfer<W> for S
3939
where
@@ -55,8 +55,9 @@ pub mod transfer {
5555

5656
/// Blocking write
5757
pub mod write {
58-
/// Default implementation of `blocking::spi::Write<W>` for implementers of `spi::FullDuplex<W>`
59-
pub trait Default<W>: crate::spi::FullDuplex<W> {}
58+
/// Default implementation of `blocking::spi::Write<W>` for implementers
59+
/// of `nonblocking::spi::FullDuplex<W>`
60+
pub trait Default<W>: crate::nb::spi::FullDuplex<W> {}
6061

6162
impl<W, S> crate::blocking::spi::Write<W> for S
6263
where
@@ -79,8 +80,8 @@ pub mod write {
7980
/// Blocking write (iterator version)
8081
pub mod write_iter {
8182
/// Default implementation of `blocking::spi::WriteIter<W>` for implementers of
82-
/// `spi::FullDuplex<W>`
83-
pub trait Default<W>: crate::spi::FullDuplex<W> {}
83+
/// `nonblocking::spi::FullDuplex<W>`
84+
pub trait Default<W>: crate::nb::spi::FullDuplex<W> {}
8485

8586
impl<W, S> crate::blocking::spi::WriteIter<W> for S
8687
where
File renamed without changes.

src/fmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! TODO write example of usage
44
use core::fmt::{Result, Write};
55

6-
impl<Word, Error> Write for dyn crate::serial::Write<Word, Error = Error> + '_
6+
impl<Word, Error> Write for dyn crate::nb::serial::Write<Word, Error = Error> + '_
77
where
88
Word: From<u8>,
99
{

src/lib.rs

+9-18
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
//! // omitted: other error variants
146146
//! }
147147
//!
148-
//! impl hal::serial::Read<u8> for Serial<USART1> {
148+
//! impl hal::nb::serial::Read<u8> for Serial<USART1> {
149149
//! type Error = Error;
150150
//!
151151
//! fn read(&mut self) -> nb::Result<u8, Error> {
@@ -167,7 +167,7 @@
167167
//! }
168168
//! }
169169
//!
170-
//! impl hal::serial::Write<u8> for Serial<USART1> {
170+
//! impl hal::nb::serial::Write<u8> for Serial<USART1> {
171171
//! type Error = Error;
172172
//!
173173
//! fn write(&mut self, byte: u8) -> nb::Result<(), Error> {
@@ -198,7 +198,7 @@
198198
//!
199199
//! ```
200200
//! use crate::stm32f1xx_hal::Serial1;
201-
//! use embedded_hal::serial::Write;
201+
//! use embedded_hal::nb::serial::Write;
202202
//! use nb::block;
203203
//!
204204
//! # fn main() {
@@ -248,7 +248,7 @@
248248
//!
249249
//! fn write_all<S>(serial: &mut S, buffer: &[u8]) -> Result<(), S::Error>
250250
//! where
251-
//! S: hal::serial::Write<u8>
251+
//! S: hal::nb::serial::Write<u8>
252252
//! {
253253
//! for &byte in buffer {
254254
//! block!(serial.write(byte))?;
@@ -281,8 +281,8 @@
281281
//! timeout: T::Time,
282282
//! ) -> Result<u8, Error<S::Error, T::Error>>
283283
//! where
284-
//! T: hal::timer::CountDown<Error = ()>,
285-
//! S: hal::serial::Read<u8>,
284+
//! T: hal::nb::timer::CountDown<Error = ()>,
285+
//! S: hal::nb::serial::Read<u8>,
286286
//! {
287287
//! timer.start(timeout).map_err(Error::TimedOut)?;
288288
//!
@@ -325,7 +325,7 @@
325325
//!
326326
//! fn flush<S>(serial: &mut S, cb: &mut CircularBuffer)
327327
//! where
328-
//! S: hal::serial::Write<u8, Error = Infallible>,
328+
//! S: hal::nb::serial::Write<u8, Error = Infallible>,
329329
//! {
330330
//! loop {
331331
//! if let Some(byte) = cb.peek() {
@@ -389,7 +389,7 @@
389389
//! # fn deref_mut(&mut self) -> &mut T { self.0 }
390390
//! # }
391391
//! # struct Serial1;
392-
//! # impl hal::serial::Write<u8> for Serial1 {
392+
//! # impl hal::nb::serial::Write<u8> for Serial1 {
393393
//! # type Error = Infallible;
394394
//! # fn write(&mut self, _: u8) -> nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) }
395395
//! # fn flush(&mut self) -> nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) }
@@ -408,19 +408,10 @@
408408
#![deny(missing_docs)]
409409
#![no_std]
410410

411-
pub mod adc;
412411
pub mod blocking;
413-
pub mod capture;
414-
pub mod digital;
415412
pub mod fmt;
413+
pub mod nb;
416414
pub mod prelude;
417-
pub mod pwm;
418-
pub mod qei;
419-
pub mod rng;
420-
pub mod serial;
421-
pub mod spi;
422-
pub mod timer;
423-
pub mod watchdog;
424415

425416
mod private {
426417
use crate::blocking::i2c::{SevenBitAddress, TenBitAddress};

src/adc.rs renamed to src/nb/adc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
///
99
/// ```
1010
/// # use core::marker::PhantomData;
11-
/// # use embedded_hal::adc::Channel;
11+
/// # use embedded_hal::nb::adc::Channel;
1212
///
1313
/// struct Adc1; // Example ADC with single bank of 8 channels
1414
/// struct Gpio1Pin1<MODE>(PhantomData<MODE>);
@@ -55,7 +55,7 @@ pub trait Channel<ADC> {
5555
/// of the request (in contrast to continuous asynchronous sampling).
5656
///
5757
/// ```
58-
/// use embedded_hal::adc::{Channel, OneShot};
58+
/// use embedded_hal::nb::adc::{Channel, OneShot};
5959
///
6060
/// struct MyAdc; // 10-bit ADC, with 5 channels
6161
/// # impl MyAdc {

src/capture.rs renamed to src/nb/capture.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
/// # impl U32Ext for u32 { fn ms(self) -> MilliSeconds { MilliSeconds(self) } }
3737
/// # struct Capture1;
3838
/// # enum Channel { _1 }
39-
/// # impl hal::capture::Capture for Capture1 {
39+
/// # impl hal::nb::capture::Capture for Capture1 {
4040
/// # type Error = Infallible;
4141
/// # type Capture = u16;
4242
/// # type Channel = Channel;

src/nb/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//! Non-blocking API
2+
//!
3+
//! These traits make use of the [`nb`] crate
4+
//! (*please go read that crate documentation before continuing*) to abstract over
5+
//! the execution model and to also provide an optional blocking operation mode.
6+
//!
7+
//! The `nb::Result` enum is used to add an [`Error::WouldBlock`] variant to the errors
8+
//! of the traits. Using this it is possible to execute actions in a non-blocking
9+
//! way.
10+
//!
11+
//! `block!`, `Result` and `Error` from the [`nb`] crate are re-exported here to avoid
12+
//! crate version mismatches. These should be used instead of importing the `nb` crate
13+
//! directly again in dependent crates.
14+
//!
15+
//! [`nb`]: https://crates.io/crates/nb
16+
17+
pub use nb::{block, Error, Result};
18+
pub mod adc;
19+
pub mod capture;
20+
pub mod rng;
21+
pub mod serial;
22+
pub mod spi;
23+
pub mod timer;

src/rng.rs renamed to src/nb/rng.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! Random Number Generator Interface
22
3-
use nb;
4-
53
/// Nonblocking stream of random bytes.
64
pub trait Read {
75
/// An enumeration of RNG errors.
File renamed without changes.
File renamed without changes.

src/timer.rs renamed to src/nb/timer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
/// # pub fn on(&mut self) {}
4848
/// # }
4949
/// # struct Timer6;
50-
/// # impl hal::timer::CountDown for Timer6 {
50+
/// # impl hal::nb::timer::CountDown for Timer6 {
5151
/// # type Error = Infallible;
5252
/// # type Time = Seconds;
5353
/// # fn start<T>(&mut self, _: T) -> Result<(), Self::Error> where T: Into<Seconds> { Ok(()) }

src/prelude.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,38 @@
33
//! The traits have been renamed to avoid collisions with other items when
44
//! performing a glob import.
55
6-
pub use crate::adc::Channel as _embedded_hal_adc_Channel;
7-
pub use crate::adc::OneShot as _embedded_hal_adc_OneShot;
86
pub use crate::blocking::delay::DelayMs as _embedded_hal_blocking_delay_DelayMs;
97
pub use crate::blocking::delay::DelayUs as _embedded_hal_blocking_delay_DelayUs;
8+
pub use crate::blocking::digital::InputPin as _embedded_hal_blocking_digital_InputPin;
9+
pub use crate::blocking::digital::OutputPin as _embedded_hal_blocking_digital_OutputPin;
10+
pub use crate::blocking::digital::StatefulOutputPin as _embedded_hal_blocking_digital_StatefulOutputPin;
11+
pub use crate::blocking::digital::ToggleableOutputPin as _embedded_hal_blocking_digital_ToggleableOutputPin;
1012
pub use crate::blocking::i2c::{
1113
Read as _embedded_hal_blocking_i2c_Read,
1214
Transactional as _embedded_hal_blocking_i2c_Transactional,
1315
Write as _embedded_hal_blocking_i2c_Write, WriteIter as _embedded_hal_blocking_i2c_WriteIter,
1416
WriteIterRead as _embedded_hal_blocking_i2c_WriteIterRead,
1517
WriteRead as _embedded_hal_blocking_i2c_WriteRead,
1618
};
19+
pub use crate::blocking::pwm::Pwm as _embedded_hal_blocking_Pwm;
20+
pub use crate::blocking::pwm::PwmPin as _embedded_hal_blocking_PwmPin;
21+
pub use crate::blocking::qei::Qei as _embedded_hal_blocking_Qei;
1722
pub use crate::blocking::rng::Read as _embedded_hal_blocking_rng_Read;
1823
pub use crate::blocking::serial::Write as _embedded_hal_blocking_serial_Write;
1924
pub use crate::blocking::spi::{
2025
Transfer as _embedded_hal_blocking_spi_Transfer, Write as _embedded_hal_blocking_spi_Write,
2126
WriteIter as _embedded_hal_blocking_spi_WriteIter,
2227
};
23-
pub use crate::capture::Capture as _embedded_hal_Capture;
24-
pub use crate::digital::InputPin as _embedded_hal_digital_InputPin;
25-
pub use crate::digital::OutputPin as _embedded_hal_digital_OutputPin;
26-
pub use crate::digital::StatefulOutputPin as _embedded_hal_digital_StatefulOutputPin;
27-
pub use crate::digital::ToggleableOutputPin as _embedded_hal_digital_ToggleableOutputPin;
28-
pub use crate::pwm::Pwm as _embedded_hal_Pwm;
29-
pub use crate::pwm::PwmPin as _embedded_hal_PwmPin;
30-
pub use crate::qei::Qei as _embedded_hal_Qei;
31-
pub use crate::rng::Read as _embedded_hal_rng_Read;
32-
pub use crate::serial::Read as _embedded_hal_serial_Read;
33-
pub use crate::serial::Write as _embedded_hal_serial_Write;
34-
pub use crate::spi::FullDuplex as _embedded_hal_spi_FullDuplex;
35-
pub use crate::timer::Cancel as _embedded_hal_timer_Cancel;
36-
pub use crate::timer::CountDown as _embedded_hal_timer_CountDown;
37-
pub use crate::timer::Periodic as _embedded_hal_timer_Periodic;
38-
pub use crate::watchdog::Disable as _embedded_hal_watchdog_Disable;
39-
pub use crate::watchdog::Enable as _embedded_hal_watchdog_Enable;
40-
pub use crate::watchdog::Watchdog as _embedded_hal_watchdog_Watchdog;
28+
pub use crate::blocking::watchdog::Disable as _embedded_hal_blocking_watchdog_Disable;
29+
pub use crate::blocking::watchdog::Enable as _embedded_hal_blocking_watchdog_Enable;
30+
pub use crate::blocking::watchdog::Watchdog as _embedded_hal_blocking_watchdog_Watchdog;
31+
pub use crate::nb::adc::Channel as _embedded_hal_nb_adc_Channel;
32+
pub use crate::nb::adc::OneShot as _embedded_hal_nb_adc_OneShot;
33+
pub use crate::nb::capture::Capture as _embedded_hal_nb_Capture;
34+
pub use crate::nb::rng::Read as _embedded_hal_nb_rng_Read;
35+
pub use crate::nb::serial::Read as _embedded_hal_nb_serial_Read;
36+
pub use crate::nb::serial::Write as _embedded_hal_nb_serial_Write;
37+
pub use crate::nb::spi::FullDuplex as _embedded_hal_nb_spi_FullDuplex;
38+
pub use crate::nb::timer::Cancel as _embedded_hal_nb_timer_Cancel;
39+
pub use crate::nb::timer::CountDown as _embedded_hal_nb_timer_CountDown;
40+
pub use crate::nb::timer::Periodic as _embedded_hal_nb_timer_Periodic;

0 commit comments

Comments
 (0)