Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. trait Spi<Word> {
  2. type Error;
  3. fn begin_transaction(&mut self) -> Result<(), Self::Error>;
  4.  
  5. /// Only valid to call during a transaction
  6. fn send(&mut self, data: Word) -> Result<(), Self::Error>;
  7.  
  8. fn end_transaction(&mut self) -> Result<(), Self::Error>;
  9. }
  10.  
  11. trait SpiTransaction<Word, Error> {
  12. fn send(&mut self, data: Word) -> Result<(), Error>;
  13. }
  14.  
  15. struct SpiWrapper<'a, S> {
  16. spi: &'a mut S
  17. }
  18.  
  19. impl <'a, Word, S: Spi<Word>> SpiTransaction<Word, S::Error> for SpiWrapper<'a, S> {
  20. fn send(&mut self, data: Word) -> Result<(), S::Error> {
  21. self.spi.send(data)
  22. }
  23. }
  24.  
  25. fn doTransaction<Word,
  26. S: Spi<Word>,
  27. F: FnOnce(&mut dyn SpiTransaction<Word, S::Error>) ->
  28. Result<(), S::Error>>(s: &mut S, f: F) -> Result<(), S::Error> {
  29. s.begin_transaction()?;
  30. f(&mut SpiWrapper{spi: s})?;
  31. s.end_transaction()
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement