Skip to content

Commit 88412b9

Browse files
bors[bot]eldruin
andauthored
Merge #192
192: [towards 1.0]: Fallible proven traits r=thejpster a=eldruin Here some work towards 1.0 following #177 (comment). Co-authored-by: Diego Barrios Romero <[email protected]>
2 parents 62a5dc6 + ee56d1e commit 88412b9

22 files changed

+231
-939
lines changed

CHANGELOG.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1111
- A nonblocking trait for interfacing with random number generation hardware.
1212

1313
### Changed
14-
- The current versions of `InputPin` have been proven. These are `digital::v1::InputPin`
15-
and `digital::v2::InputPin`.
14+
- All traits have been marked as proven (`unproven` feature has been removed).
15+
- All trait methods have been made fallible.
16+
- All trait methods have been renamed `try_*` (i.e. `try_send`) for consistency.
17+
- The minimum supported Rust version is 1.35 due to [this issue](https://github.com/rust-lang/rust/issues/54973).
1618

1719
## [v0.2.3] - 2019-05-09
1820

Cargo.toml

+2-7
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,10 @@ readme = "README.md"
1414
repository = "https://github.com/rust-embedded/embedded-hal"
1515
version = "0.2.3"
1616

17-
[dependencies.nb]
18-
version = "0.1.1"
17+
[dependencies]
18+
nb = { version = "0.1.1", features = ["unstable"] }
1919

2020
[dev-dependencies]
2121
stm32f3 = { version = "0.8", features = ["stm32f303", "rt"] }
2222
futures = "0.1.17"
2323

24-
[features]
25-
unproven = ["nb/unstable"]
26-
27-
[package.metadata.docs.rs]
28-
features = ["unproven"]

ci/script.sh

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ set -euxo pipefail
22

33
main() {
44
cargo check --target $TARGET
5-
cargo check --target $TARGET --features unproven
65
cargo fmt -- --check
76

87
if [ $TRAVIS_RUST_VERSION = nightly ]; then
9-
cargo test --target $TARGET --features unproven
8+
cargo test --target $TARGET
109
fi
1110
}
1211

src/adc.rs

+7-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Analog-digital conversion traits
22
3-
#[cfg(feature = "unproven")]
43
use nb;
54

65
/// A marker trait to identify MCU pins that can be used as inputs to an ADC channel.
@@ -10,7 +9,7 @@ use nb;
109
/// between the physical interface and the ADC sampling buffer.
1110
///
1211
/// ```
13-
/// # use std::marker::PhantomData;
12+
/// # use core::marker::PhantomData;
1413
/// # use embedded_hal::adc::Channel;
1514
///
1615
/// struct Adc1; // Example ADC with single bank of 8 channels
@@ -21,7 +20,7 @@ use nb;
2120
/// impl Channel<Adc1> for Gpio1Pin1<Analog> {
2221
/// type ID = u8; // ADC channels are identified numerically
2322
///
24-
/// fn channel() -> u8 { 7_u8 } // GPIO pin 1 is connected to ADC channel 7
23+
/// const CHANNEL: u8 = 7_u8; // GPIO pin 1 is connected to ADC channel 7
2524
/// }
2625
///
2726
/// struct Adc2; // ADC with two banks of 16 channels
@@ -32,10 +31,9 @@ use nb;
3231
/// impl Channel<Adc2> for Gpio2PinA<AltFun> {
3332
/// type ID = (u8, u8); // ADC channels are identified by bank number and channel number
3433
///
35-
/// fn channel() -> (u8, u8) { (0, 3) } // bank 0 channel 3
34+
/// const CHANNEL: (u8, u8) = (0, 3); // bank 0 channel 3
3635
/// }
3736
/// ```
38-
#[cfg(feature = "unproven")]
3937
pub trait Channel<ADC> {
4038
/// Channel ID type
4139
///
@@ -46,12 +44,7 @@ pub trait Channel<ADC> {
4644

4745
/// Get the specific ID that identifies this channel, for example `0_u8` for the first ADC
4846
/// channel, if Self::ID is u8.
49-
fn channel() -> Self::ID;
50-
51-
// `channel` is a function due to [this reported
52-
// issue](https://github.com/rust-lang/rust/issues/54973). Something about blanket impls
53-
// combined with `type ID; const CHANNEL: Self::ID;` causes problems.
54-
//const CHANNEL: Self::ID;
47+
const CHANNEL: Self::ID;
5548
}
5649

5750
/// ADCs that sample on single channels per request, and do so at the time of the request.
@@ -76,16 +69,15 @@ pub trait Channel<ADC> {
7669
/// {
7770
/// type Error = ();
7871
///
79-
/// fn read(&mut self, _pin: &mut PIN) -> nb::Result<WORD, Self::Error> {
80-
/// let chan = 1 << PIN::channel();
72+
/// fn try_read(&mut self, _pin: &mut PIN) -> nb::Result<WORD, Self::Error> {
73+
/// let chan = 1 << PIN::CHANNEL;
8174
/// self.power_up();
8275
/// let result = self.do_conversion(chan);
8376
/// self.power_down();
8477
/// Ok(result.into())
8578
/// }
8679
/// }
8780
/// ```
88-
#[cfg(feature = "unproven")]
8981
pub trait OneShot<ADC, Word, Pin: Channel<ADC>> {
9082
/// Error type returned by ADC methods
9183
type Error;
@@ -94,5 +86,5 @@ pub trait OneShot<ADC, Word, Pin: Channel<ADC>> {
9486
///
9587
/// This method takes a `Pin` reference, as it is expected that the ADC will be able to sample
9688
/// whatever channel underlies the pin.
97-
fn read(&mut self, pin: &mut Pin) -> nb::Result<Word, Self::Error>;
89+
fn try_read(&mut self, pin: &mut Pin) -> nb::Result<Word, Self::Error>;
9890
}

src/blocking/delay.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,21 @@
1212
/// `UXX` denotes the range type of the delay time. `UXX` can be `u8`, `u16`, etc. A single type can
1313
/// implement this trait for different types of `UXX`.
1414
pub trait DelayMs<UXX> {
15+
/// Enumeration of `DelayMs` errors
16+
type Error;
17+
1518
/// Pauses execution for `ms` milliseconds
16-
fn delay_ms(&mut self, ms: UXX);
19+
fn try_delay_ms(&mut self, ms: UXX) -> Result<(), Self::Error>;
1720
}
1821

1922
/// Microsecond delay
2023
///
2124
/// `UXX` denotes the range type of the delay time. `UXX` can be `u8`, `u16`, etc. A single type can
2225
/// implement this trait for different types of `UXX`.
2326
pub trait DelayUs<UXX> {
27+
/// Enumeration of `DelayMs` errors
28+
type Error;
29+
2430
/// Pauses execution for `us` microseconds
25-
fn delay_us(&mut self, us: UXX);
31+
fn try_delay_us(&mut self, us: UXX) -> Result<(), Self::Error>;
2632
}

src/blocking/i2c.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub trait Read {
2828
/// - `MAK` = master acknowledge
2929
/// - `NMAK` = master no acknowledge
3030
/// - `SP` = stop condition
31-
fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error>;
31+
fn try_read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error>;
3232
}
3333

3434
/// Blocking write
@@ -52,11 +52,10 @@ pub trait Write {
5252
/// - `SAK` = slave acknowledge
5353
/// - `Bi` = ith byte of data
5454
/// - `SP` = stop condition
55-
fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error>;
55+
fn try_write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error>;
5656
}
5757

5858
/// Blocking write (iterator version)
59-
#[cfg(feature = "unproven")]
6059
pub trait WriteIter {
6160
/// Error type
6261
type Error;
@@ -66,7 +65,7 @@ pub trait WriteIter {
6665
/// # I2C Events (contract)
6766
///
6867
/// Same as `Write`
69-
fn write<B>(&mut self, addr: u8, bytes: B) -> Result<(), Self::Error>
68+
fn try_write<B>(&mut self, addr: u8, bytes: B) -> Result<(), Self::Error>
7069
where
7170
B: IntoIterator<Item = u8>;
7271
}
@@ -98,7 +97,7 @@ pub trait WriteRead {
9897
/// - `MAK` = master acknowledge
9998
/// - `NMAK` = master no acknowledge
10099
/// - `SP` = stop condition
101-
fn write_read(
100+
fn try_write_read(
102101
&mut self,
103102
address: u8,
104103
bytes: &[u8],
@@ -107,7 +106,6 @@ pub trait WriteRead {
107106
}
108107

109108
/// Blocking write (iterator version) + read
110-
#[cfg(feature = "unproven")]
111109
pub trait WriteIterRead {
112110
/// Error type
113111
type Error;
@@ -118,7 +116,7 @@ pub trait WriteIterRead {
118116
/// # I2C Events (contract)
119117
///
120118
/// Same as the `WriteRead` trait
121-
fn write_iter_read<B>(
119+
fn try_write_iter_read<B>(
122120
&mut self,
123121
address: u8,
124122
bytes: B,

src/blocking/rng.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//! Blocking hardware random number generator
22
33
/// Blocking read
4-
///
5-
/// *This trait is available if embedded-hal is built with the `"unproven"` feature.*
6-
#[cfg(feature = "unproven")]
74
pub trait Read {
85
/// Error type
96
type Error;
@@ -15,5 +12,5 @@ pub trait Read {
1512
///
1613
/// If this function returns an error, it is unspecified how many bytes it has read, but it
1714
/// will never read more than would be necessary to completely fill the buffer.
18-
fn read(&mut self, buffer: &mut [u8]) -> Result<(), Self::Error>;
15+
fn try_read(&mut self, buffer: &mut [u8]) -> Result<(), Self::Error>;
1916
}

src/blocking/serial.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ pub trait Write<Word> {
1010
/// An implementation can choose to buffer the write, returning `Ok(())`
1111
/// after the complete slice has been written to a buffer, but before all
1212
/// words have been sent via the serial interface. To make sure that
13-
/// everything has been sent, call [`bflush`] after this function returns.
13+
/// everything has been sent, call [`try_bflush`] after this function returns.
1414
///
15-
/// [`bflush`]: #tymethod.bflush
16-
fn bwrite_all(&mut self, buffer: &[Word]) -> Result<(), Self::Error>;
15+
/// [`try_bflush`]: #tymethod.bflush
16+
fn try_bwrite_all(&mut self, buffer: &[Word]) -> Result<(), Self::Error>;
1717

1818
/// Block until the serial interface has sent all buffered words
19-
fn bflush(&mut self) -> Result<(), Self::Error>;
19+
fn try_bflush(&mut self) -> Result<(), Self::Error>;
2020
}
2121

2222
/// Blocking serial write
@@ -38,16 +38,16 @@ pub mod write {
3838
{
3939
type Error = S::Error;
4040

41-
fn bwrite_all(&mut self, buffer: &[Word]) -> Result<(), Self::Error> {
41+
fn try_bwrite_all(&mut self, buffer: &[Word]) -> Result<(), Self::Error> {
4242
for word in buffer {
43-
block!(self.write(word.clone()))?;
43+
block!(self.try_write(word.clone()))?;
4444
}
4545

4646
Ok(())
4747
}
4848

49-
fn bflush(&mut self) -> Result<(), Self::Error> {
50-
block!(self.flush())?;
49+
fn try_bflush(&mut self) -> Result<(), Self::Error> {
50+
block!(self.try_flush())?;
5151
Ok(())
5252
}
5353
}

src/blocking/spi.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub trait Transfer<W> {
66
type Error;
77

88
/// Sends `words` to the slave. Returns the `words` received from the slave
9-
fn transfer<'w>(&mut self, words: &'w mut [W]) -> Result<&'w [W], Self::Error>;
9+
fn try_transfer<'w>(&mut self, words: &'w mut [W]) -> Result<&'w [W], Self::Error>;
1010
}
1111

1212
/// Blocking write
@@ -15,17 +15,16 @@ pub trait Write<W> {
1515
type Error;
1616

1717
/// Sends `words` to the slave, ignoring all the incoming words
18-
fn write(&mut self, words: &[W]) -> Result<(), Self::Error>;
18+
fn try_write(&mut self, words: &[W]) -> Result<(), Self::Error>;
1919
}
2020

2121
/// Blocking write (iterator version)
22-
#[cfg(feature = "unproven")]
2322
pub trait WriteIter<W> {
2423
/// Error type
2524
type Error;
2625

2726
/// Sends `words` to the slave, ignoring all the incoming words
28-
fn write_iter<WI>(&mut self, words: WI) -> Result<(), Self::Error>
27+
fn try_write_iter<WI>(&mut self, words: WI) -> Result<(), Self::Error>
2928
where
3029
WI: IntoIterator<Item = W>;
3130
}
@@ -43,10 +42,10 @@ pub mod transfer {
4342
{
4443
type Error = S::Error;
4544

46-
fn transfer<'w>(&mut self, words: &'w mut [W]) -> Result<&'w [W], S::Error> {
45+
fn try_transfer<'w>(&mut self, words: &'w mut [W]) -> Result<&'w [W], S::Error> {
4746
for word in words.iter_mut() {
48-
block!(self.send(word.clone()))?;
49-
*word = block!(self.read())?;
47+
block!(self.try_send(word.clone()))?;
48+
*word = block!(self.try_read())?;
5049
}
5150

5251
Ok(words)
@@ -66,10 +65,10 @@ pub mod write {
6665
{
6766
type Error = S::Error;
6867

69-
fn write(&mut self, words: &[W]) -> Result<(), S::Error> {
68+
fn try_write(&mut self, words: &[W]) -> Result<(), S::Error> {
7069
for word in words {
71-
block!(self.send(word.clone()))?;
72-
block!(self.read())?;
70+
block!(self.try_send(word.clone()))?;
71+
block!(self.try_read())?;
7372
}
7473

7574
Ok(())
@@ -78,7 +77,6 @@ pub mod write {
7877
}
7978

8079
/// Blocking write (iterator version)
81-
#[cfg(feature = "unproven")]
8280
pub mod write_iter {
8381
/// Default implementation of `blocking::spi::WriteIter<W>` for implementers of
8482
/// `spi::FullDuplex<W>`
@@ -91,13 +89,13 @@ pub mod write_iter {
9189
{
9290
type Error = S::Error;
9391

94-
fn write_iter<WI>(&mut self, words: WI) -> Result<(), S::Error>
92+
fn try_write_iter<WI>(&mut self, words: WI) -> Result<(), S::Error>
9593
where
9694
WI: IntoIterator<Item = W>,
9795
{
9896
for word in words.into_iter() {
99-
block!(self.send(word.clone()))?;
100-
block!(self.read())?;
97+
block!(self.try_send(word.clone()))?;
98+
block!(self.try_read())?;
10199
}
102100

103101
Ok(())

0 commit comments

Comments
 (0)