Skip to content

Commit 7f3801e

Browse files
Merge #289
289: Remove the various Default traits r=therealprof a=lachlansneff As discussed in the embedded-hal meeting, the default traits and their corresponding impls interfere with upstream implementations. Co-authored-by: Lachlan Sneff <[email protected]> Co-authored-by: Lachlan Sneff <[email protected]>
2 parents 8940cfd + 9bbfe85 commit 7f3801e

File tree

5 files changed

+1
-289
lines changed

5 files changed

+1
-289
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1919
importing the `nb` crate directly in dependendent crates.
2020
- `blocking::Serial`: renamed `bwrite_all` to `write`, `bflush` to `flush.
2121
- Removed `prelude` to avoid method name conflicts between different flavors (blocking, nb) of the same trait. Traits must now be manually imported.
22+
- Removed the various `Default` marker traits.
2223

2324
### Removed
2425
- Removed random number generation (`rng`) traits in favor of [rand_core](https://crates.io/crates/rand_core).

src/blocking/digital.rs

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -96,73 +96,6 @@ pub trait ToggleableOutputPin {
9696
fn toggle(&mut self) -> Result<(), Self::Error>;
9797
}
9898

99-
/// If you can read **and** write the output state, a pin is
100-
/// toggleable by software.
101-
///
102-
/// ```
103-
/// use embedded_hal::blocking::digital::{OutputPin, StatefulOutputPin, ToggleableOutputPin};
104-
/// use embedded_hal::blocking::digital::toggleable;
105-
/// use core::convert::Infallible;
106-
///
107-
/// /// A virtual output pin that exists purely in software
108-
/// struct MyPin {
109-
/// state: bool
110-
/// }
111-
///
112-
/// impl OutputPin for MyPin {
113-
/// type Error = Infallible;
114-
///
115-
/// fn set_low(&mut self) -> Result<(), Self::Error> {
116-
/// self.state = false;
117-
/// Ok(())
118-
/// }
119-
/// fn set_high(&mut self) -> Result<(), Self::Error> {
120-
/// self.state = true;
121-
/// Ok(())
122-
/// }
123-
/// }
124-
///
125-
/// impl StatefulOutputPin for MyPin {
126-
/// fn is_set_low(&self) -> Result<bool, Self::Error> {
127-
/// Ok(!self.state)
128-
/// }
129-
/// fn is_set_high(&self) -> Result<bool, Self::Error> {
130-
/// Ok(self.state)
131-
/// }
132-
/// }
133-
///
134-
/// /// Opt-in to the software implementation.
135-
/// impl toggleable::Default for MyPin {}
136-
///
137-
/// let mut pin = MyPin { state: false };
138-
/// pin.toggle().unwrap();
139-
/// assert!(pin.is_set_high().unwrap());
140-
/// pin.toggle().unwrap();
141-
/// assert!(pin.is_set_low().unwrap());
142-
/// ```
143-
pub mod toggleable {
144-
use super::{OutputPin, StatefulOutputPin, ToggleableOutputPin};
145-
146-
/// Software-driven `toggle()` implementation.
147-
pub trait Default: OutputPin + StatefulOutputPin {}
148-
149-
impl<P> ToggleableOutputPin for P
150-
where
151-
P: Default,
152-
{
153-
type Error = P::Error;
154-
155-
/// Toggle pin output
156-
fn toggle(&mut self) -> Result<(), Self::Error> {
157-
if self.is_set_low()? {
158-
self.set_high()
159-
} else {
160-
self.set_low()
161-
}
162-
}
163-
}
164-
}
165-
16699
/// Single digital input pin
167100
pub trait InputPin {
168101
/// Error type

src/blocking/i2c.rs

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -292,88 +292,3 @@ pub trait TransactionalIter<A: AddressMode = SevenBitAddress> {
292292
where
293293
O: IntoIterator<Item = Operation<'a>>;
294294
}
295-
296-
/// Default implementation of `blocking::i2c::Write`, `blocking::i2c::Read` and
297-
/// `blocking::i2c::WriteRead` traits for `blocking::i2c::Transactional` implementers.
298-
///
299-
/// If you implement `blocking::i2c::Transactional` for your I2C peripheral,
300-
/// you can use this default implementation so that you do not need to implement
301-
/// the `blocking::i2c::Write`, `blocking::i2c::Read` and `blocking::i2c::WriteRead`
302-
/// traits as well.
303-
/// ```
304-
/// use embedded_hal::blocking::i2c;
305-
///
306-
/// struct I2c1;
307-
///
308-
/// impl i2c::Transactional<i2c::SevenBitAddress> for I2c1 {
309-
/// # type Error = ();
310-
/// fn exec<'a>(
311-
/// &mut self,
312-
/// address: i2c::SevenBitAddress,
313-
/// operations: &mut [i2c::Operation<'a>],
314-
/// ) -> Result<(), Self::Error> {
315-
/// // ...
316-
/// # Ok(())
317-
/// }
318-
/// }
319-
///
320-
/// // This is all you need to do:
321-
/// impl i2c::transactional::Default<i2c::SevenBitAddress> for I2c1 {};
322-
///
323-
/// // Then you can use `Write` and so on:
324-
/// use i2c::Write;
325-
///
326-
/// let mut i2c1 = I2c1{};
327-
/// i2c1.write(0x01, &[0xAB, 0xCD]).unwrap();
328-
/// ```
329-
pub mod transactional {
330-
use super::{AddressMode, Operation, Read, Transactional, Write, WriteRead};
331-
332-
/// Default implementation of `blocking::i2c::Write`, `blocking::i2c::Read` and
333-
/// `blocking::i2c::WriteRead` traits for `blocking::i2c::Transactional` implementers.
334-
pub trait Default<A: AddressMode>: Transactional<A> {}
335-
336-
impl<A, E, S> Write<A> for S
337-
where
338-
A: AddressMode,
339-
S: self::Default<A> + Transactional<A, Error = E>,
340-
{
341-
type Error = E;
342-
343-
fn write(&mut self, address: A, bytes: &[u8]) -> Result<(), Self::Error> {
344-
self.exec(address, &mut [Operation::Write(bytes)])
345-
}
346-
}
347-
348-
impl<A, E, S> Read<A> for S
349-
where
350-
A: AddressMode,
351-
S: self::Default<A> + Transactional<A, Error = E>,
352-
{
353-
type Error = E;
354-
355-
fn read(&mut self, address: A, buffer: &mut [u8]) -> Result<(), Self::Error> {
356-
self.exec(address, &mut [Operation::Read(buffer)])
357-
}
358-
}
359-
360-
impl<A, E, S> WriteRead<A> for S
361-
where
362-
A: AddressMode,
363-
S: self::Default<A> + Transactional<A, Error = E>,
364-
{
365-
type Error = E;
366-
367-
fn write_read(
368-
&mut self,
369-
address: A,
370-
bytes: &[u8],
371-
buffer: &mut [u8],
372-
) -> Result<(), Self::Error> {
373-
self.exec(
374-
address,
375-
&mut [Operation::Write(bytes), Operation::Read(buffer)],
376-
)
377-
}
378-
}
379-
}

src/blocking/serial.rs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,3 @@ pub trait Write<Word> {
1818
/// Block until the serial interface has sent all buffered words
1919
fn flush(&mut self) -> Result<(), Self::Error>;
2020
}
21-
22-
/// Blocking serial write
23-
pub mod write {
24-
/// Marker trait to opt into default blocking write implementation
25-
///
26-
/// Implementers of [`nonblocking::serial::Write`] can implement this marker trait
27-
/// for their type. Doing so will automatically provide the default
28-
/// implementation of [`blocking::serial::Write`] for the type.
29-
///
30-
/// [`nonblocking::serial::Write`]: ../../nonblocking/serial/trait.Write.html
31-
/// [`blocking::serial::Write`]: ../trait.Write.html
32-
pub trait Default<Word>: crate::nb::serial::Write<Word> {}
33-
34-
impl<S, Word> crate::blocking::serial::Write<Word> for S
35-
where
36-
S: Default<Word>,
37-
Word: Clone,
38-
{
39-
type Error = S::Error;
40-
41-
fn write(&mut self, buffer: &[Word]) -> Result<(), Self::Error> {
42-
for word in buffer {
43-
nb::block!(self.write(word.clone()))?;
44-
}
45-
46-
Ok(())
47-
}
48-
49-
fn flush(&mut self) -> Result<(), Self::Error> {
50-
nb::block!(self.flush())?;
51-
Ok(())
52-
}
53-
}
54-
}

src/blocking/spi.rs

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -29,81 +29,6 @@ pub trait WriteIter<W> {
2929
WI: IntoIterator<Item = W>;
3030
}
3131

32-
/// Blocking transfer
33-
pub mod transfer {
34-
/// Default implementation of `blocking::spi::Transfer<W>` for implementers of
35-
/// `nonblocking::spi::FullDuplex<W>`
36-
pub trait Default<W>: crate::nb::spi::FullDuplex<W> {}
37-
38-
impl<W, S> crate::blocking::spi::Transfer<W> for S
39-
where
40-
S: Default<W>,
41-
W: Clone,
42-
{
43-
type Error = S::Error;
44-
45-
fn transfer<'w>(&mut self, words: &'w mut [W]) -> Result<&'w [W], S::Error> {
46-
for word in words.iter_mut() {
47-
nb::block!(self.write(word.clone()))?;
48-
*word = nb::block!(self.read())?;
49-
}
50-
51-
Ok(words)
52-
}
53-
}
54-
}
55-
56-
/// Blocking write
57-
pub mod write {
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> {}
61-
62-
impl<W, S> crate::blocking::spi::Write<W> for S
63-
where
64-
S: Default<W>,
65-
W: Clone,
66-
{
67-
type Error = S::Error;
68-
69-
fn write(&mut self, words: &[W]) -> Result<(), S::Error> {
70-
for word in words {
71-
nb::block!(self.write(word.clone()))?;
72-
nb::block!(self.read())?;
73-
}
74-
75-
Ok(())
76-
}
77-
}
78-
}
79-
80-
/// Blocking write (iterator version)
81-
pub mod write_iter {
82-
/// Default implementation of `blocking::spi::WriteIter<W>` for implementers of
83-
/// `nonblocking::spi::FullDuplex<W>`
84-
pub trait Default<W>: crate::nb::spi::FullDuplex<W> {}
85-
86-
impl<W, S> crate::blocking::spi::WriteIter<W> for S
87-
where
88-
S: Default<W>,
89-
W: Clone,
90-
{
91-
type Error = S::Error;
92-
93-
fn write_iter<WI>(&mut self, words: WI) -> Result<(), S::Error>
94-
where
95-
WI: IntoIterator<Item = W>,
96-
{
97-
for word in words.into_iter() {
98-
nb::block!(self.write(word.clone()))?;
99-
nb::block!(self.read())?;
100-
}
101-
102-
Ok(())
103-
}
104-
}
105-
}
106-
10732
/// Operation for transactional SPI trait
10833
///
10934
/// This allows composition of SPI operations into a single bus transaction
@@ -124,31 +49,3 @@ pub trait Transactional<W: 'static> {
12449
/// Execute the provided transactions
12550
fn exec<'a>(&mut self, operations: &mut [Operation<'a, W>]) -> Result<(), Self::Error>;
12651
}
127-
128-
/// Blocking transactional impl over spi::Write and spi::Transfer
129-
pub mod transactional {
130-
use super::{Operation, Transfer, Write};
131-
132-
/// Default implementation of `blocking::spi::Transactional<W>` for implementers of
133-
/// `spi::Write<W>` and `spi::Transfer<W>`
134-
pub trait Default<W>: Write<W> + Transfer<W> {}
135-
136-
impl<W: 'static, E, S> super::Transactional<W> for S
137-
where
138-
S: self::Default<W> + Write<W, Error = E> + Transfer<W, Error = E>,
139-
W: Copy + Clone,
140-
{
141-
type Error = E;
142-
143-
fn exec<'a>(&mut self, operations: &mut [super::Operation<'a, W>]) -> Result<(), E> {
144-
for op in operations {
145-
match op {
146-
Operation::Write(w) => self.write(w)?,
147-
Operation::Transfer(t) => self.transfer(t).map(|_| ())?,
148-
}
149-
}
150-
151-
Ok(())
152-
}
153-
}
154-
}

0 commit comments

Comments
 (0)