Skip to content

Commit 4773acb

Browse files
committed
Default traits
1 parent db91212 commit 4773acb

File tree

1 file changed

+52
-15
lines changed

1 file changed

+52
-15
lines changed

src/blocking/spi.rs

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,66 @@
11
//! Blocking SPI API
22
3-
pub use spi::{Mode, Phase, Polarity};
4-
5-
/// Blocking full duplex
6-
pub trait FullDuplex<W> {
7-
/// An enumeration of SPI errors
3+
/// Blocking transfer
4+
pub trait Transfer<W> {
5+
/// Error type
86
type Error;
97

108
/// Sends `words` to the slave. Returns the `words` received from the slave
119
fn transfer<'w>(&mut self, words: &'w mut [W]) -> Result<&'w [W], Self::Error>;
10+
}
11+
12+
/// Blocking write
13+
pub trait Write<W> {
14+
/// Error type
15+
type Error;
1216

1317
/// Sends `words` to the slave, ignoring all the incoming words
1418
fn write(&mut self, words: &[W]) -> Result<(), Self::Error>;
1519
}
1620

17-
/// Transfers words to the slave, returns the words received from the slave
18-
pub fn transfer<'w, S, W>(spi: &mut S, words: &'w mut [W]) -> Result<&'w [W], S::Error>
19-
where
20-
S: ::spi::FullDuplex<W>,
21-
W: Clone,
22-
{
23-
for word in words.iter_mut() {
24-
block!(spi.send(word.clone()))?;
25-
*word = block!(spi.read())?;
21+
/// Blocking transfer
22+
pub mod transfer {
23+
/// Default implementation of `blocking::spi::Transfer<W>` for implementers of
24+
/// `spi::FullDuplex<W>`
25+
pub trait Default<W>: ::spi::FullDuplex<W> {}
26+
27+
impl<W, S> ::blocking::spi::Transfer<W> for S
28+
where
29+
S: Default<W>,
30+
W: Clone,
31+
{
32+
type Error = S::Error;
33+
34+
fn transfer<'w>(&mut self, words: &'w mut [W]) -> Result<&'w [W], S::Error> {
35+
for word in words.iter_mut() {
36+
block!(self.send(word.clone()))?;
37+
*word = block!(self.read())?;
38+
}
39+
40+
Ok(words)
41+
}
2642
}
43+
}
44+
45+
/// Blocking write
46+
pub mod write {
47+
/// Default implementation of `blocking::spi::Write<W>` for implementers of `spi::FullDuplex<W>`
48+
pub trait Default<W>: ::spi::FullDuplex<W> {}
2749

28-
Ok(words)
50+
impl<W, S> ::blocking::spi::Write<W> for S
51+
where
52+
S: Default<W>,
53+
W: Clone,
54+
{
55+
type Error = S::Error;
56+
57+
fn write(&mut self, words: &[W]) -> Result<(), S::Error> {
58+
for word in words {
59+
block!(self.send(word.clone()))?;
60+
block!(self.read())?;
61+
}
62+
63+
Ok(())
64+
}
65+
}
2966
}

0 commit comments

Comments
 (0)