Skip to content

Commit 48d9c82

Browse files
authored
Merge pull request #47 from japaric/write-iter
[RFC] add iterator based blocking `Write` traits
2 parents 99a0f4d + 137b473 commit 48d9c82

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/blocking/i2c.rs

+38
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ pub trait Write {
5555
fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error>;
5656
}
5757

58+
/// Blocking write (iterator version)
59+
#[cfg(feature = "unproven")]
60+
pub trait WriteIter {
61+
/// Error type
62+
type Error;
63+
64+
/// Sends bytes to slave with address `addr`
65+
///
66+
/// # I2C Events (contract)
67+
///
68+
/// Same as `Write`
69+
fn write<B>(&mut self, addr: u8, bytes: B) -> Result<(), Self::Error>
70+
where
71+
B: IntoIterator<Item = u8>;
72+
}
73+
5874
/// Blocking write + read
5975
pub trait WriteRead {
6076
/// Error type
@@ -89,3 +105,25 @@ pub trait WriteRead {
89105
buffer: &mut [u8],
90106
) -> Result<(), Self::Error>;
91107
}
108+
109+
/// Blocking write (iterator version) + read
110+
#[cfg(feature = "unproven")]
111+
pub trait WriteIterRead {
112+
/// Error type
113+
type Error;
114+
115+
/// Sends bytes to slave with address `addr` and then reads enough bytes to fill `buffer` *in a
116+
/// single transaction*
117+
///
118+
/// # I2C Events (contract)
119+
///
120+
/// Same as the `WriteRead` trait
121+
fn write_iter_read<B>(
122+
&mut self,
123+
address: u8,
124+
bytes: B,
125+
buffer: &mut [u8],
126+
) -> Result<(), Self::Error>
127+
where
128+
B: IntoIterator<Item = u8>;
129+
}

src/blocking/spi.rs

+40
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ pub trait Write<W> {
1818
fn write(&mut self, words: &[W]) -> Result<(), Self::Error>;
1919
}
2020

21+
/// Blocking write (iterator version)
22+
#[cfg(feature = "unproven")]
23+
pub trait WriteIter<W> {
24+
/// Error type
25+
type Error;
26+
27+
/// Sends `words` to the slave, ignoring all the incoming words
28+
fn write_iter<WI>(&mut self, words: WI) -> Result<(), Self::Error>
29+
where
30+
WI: IntoIterator<Item = W>;
31+
}
32+
2133
/// Blocking transfer
2234
pub mod transfer {
2335
/// Default implementation of `blocking::spi::Transfer<W>` for implementers of
@@ -64,3 +76,31 @@ pub mod write {
6476
}
6577
}
6678
}
79+
80+
/// Blocking write (iterator version)
81+
#[cfg(feature = "unproven")]
82+
pub mod write_iter {
83+
/// Default implementation of `blocking::spi::WriteIter<W>` for implementers of
84+
/// `spi::FullDuplex<W>`
85+
pub trait Default<W>: ::spi::FullDuplex<W> {}
86+
87+
impl<W, S> ::blocking::spi::WriteIter<W> for S
88+
where
89+
S: Default<W>,
90+
W: Clone,
91+
{
92+
type Error = S::Error;
93+
94+
fn write_iter<WI>(&mut self, words: WI) -> Result<(), S::Error>
95+
where
96+
WI: IntoIterator<Item = W>,
97+
{
98+
for word in words.into_iter() {
99+
block!(self.send(word.clone()))?;
100+
block!(self.read())?;
101+
}
102+
103+
Ok(())
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)