Skip to content

Commit d9b7a86

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

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/blocking/spi.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,32 @@ 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, Transactional, Transfer, Write};
132+
133+
/// Default implementation of `blocking::spi::Transactional<W>` for implementers of
134+
/// `spi::Write<W>` and `spi::Transfer<W>`
135+
pub trait Default<W, E>: Transfer<W, Error=E> + Write<W, Error=E> {}
136+
137+
impl<W: 'static, E, S> Transactional<W> for S
138+
where
139+
S: Default<W, E> + Transfer<W, Error = E> + Write<W, Error = E>,
140+
W: Clone,
141+
E: core::fmt::Debug,
142+
{
143+
type Error = E;
144+
145+
fn exec<'a>(&mut self, operations: &mut [super::Operation<'a, W>]) -> Result<(), E> {
146+
for op in operations {
147+
match op {
148+
Operation::Write(w) => Write::write(self, w)?,
149+
Operation::Transfer(t) => Transfer::transfer(self, t).map(|_| ())?,
150+
}
151+
}
152+
153+
Ok(())
154+
}
155+
}
156+
}

0 commit comments

Comments
 (0)