Skip to content

Commit 449f091

Browse files
committed
added opt-in default impl for spi::transactional
1 parent 0d85339 commit 449f091

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/blocking/spi.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,31 @@ pub trait Transactional<W: 'static> {
125125
/// Execute the provided transactions
126126
fn exec<'a>(&mut self, operations: &mut [Operation<'a, W>]) -> Result<(), Self::Error>;
127127
}
128+
129+
/// Blocking transactional impl over spi::Write and spi::Transfer
130+
pub mod transactional {
131+
use super::Operation;
132+
133+
/// Default implementation of `blocking::spi::Transactional<W>` for implementers of
134+
/// `spi::Write<W>` and `spi::Transfer<W>`
135+
pub trait Default: {}
136+
137+
impl<W: 'static, E, S> crate::blocking::spi::Transactional<W> for S
138+
where
139+
S: self::Default + crate::blocking::spi::Transfer<W, Error=E> + crate::blocking::spi::Write<W, Error=E>,
140+
W: Clone,
141+
{
142+
type Error = E;
143+
144+
fn exec<'a>(&mut self, operations: &mut [super::Operation<'a, W>]) -> Result<(), Self::Error> {
145+
for op in operations {
146+
match op {
147+
Operation::Write(w) => self.write(w)?,
148+
Operation::Transfer(t) => self.transfer(t).map(|_| ())?,
149+
}
150+
}
151+
152+
Ok(())
153+
}
154+
}
155+
}

0 commit comments

Comments
 (0)