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>;
- }
- struct Transaction<'a, S> {
- spi: &'a mut S,
- }
- impl<'a, S> Transaction<'a, S> {
- fn send<Word>(&mut self, data: Word) -> Result<(), S::Error>
- where
- S: Spi<Word>,
- {
- self.spi.send(data)
- }
- }
- fn doTransaction<
- Word,
- S: Spi<Word>,
- F: FnOnce(Transaction<S>) -> Result<(), S::Error>,
- >(
- s: &mut S,
- f: F,
- ) -> Result<(), S::Error> {
- s.begin_transaction()?;
- f(Transaction { spi: s })?;
- s.end_transaction()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement