Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait Spi<Word> {
- type Error;
- fn begin_transaction(&mut self) -> Result<(), Self::Error>;
- /// Only valid to call during a transaction
- fn send(&mut self, data: Word) -> Result<(), Self::Error>;
- fn end_transaction(&mut self) -> Result<(), Self::Error>;
- }
- trait SpiTransaction<Word, Error> {
- fn send(&mut self, data: Word) -> Result<(), Error>;
- }
- struct SpiWrapper<'a, S> {
- spi: &'a mut S
- }
- impl <'a, Word, S: Spi<Word>> SpiTransaction<Word, S::Error> for SpiWrapper<'a, S> {
- fn send(&mut self, data: Word) -> Result<(), S::Error> {
- self.spi.send(data)
- }
- }
- fn doTransaction<Word,
- S: Spi<Word>,
- F: FnOnce(&mut dyn SpiTransaction<Word, S::Error>) ->
- Result<(), S::Error>>(s: &mut S, f: F) -> Result<(), S::Error> {
- s.begin_transaction()?;
- f(&mut SpiWrapper{spi: s})?;
- s.end_transaction()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement