Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 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. struct Transaction<'a, S> {
  12. spi: &'a mut S,
  13. }
  14.  
  15. impl<'a, S> Transaction<'a, S> {
  16. fn send<Word>(&mut self, data: Word) -> Result<(), S::Error>
  17. where
  18. S: Spi<Word>,
  19. {
  20. self.spi.send(data)
  21. }
  22. }
  23.  
  24. fn doTransaction<
  25. Word,
  26. S: Spi<Word>,
  27. F: FnOnce(Transaction<S>) -> Result<(), S::Error>,
  28. >(
  29. s: &mut S,
  30. f: F,
  31. ) -> Result<(), S::Error> {
  32. s.begin_transaction()?;
  33. f(Transaction { spi: s })?;
  34. s.end_transaction()
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement