embedded_hal/blocking/
serial.rs

1//! Blocking serial API
2
3/// Write half of a serial interface (blocking variant)
4pub trait Write<Word> {
5    /// The type of error that can occur when writing
6    type Error;
7
8    /// Writes a slice, blocking until everything has been written
9    ///
10    /// An implementation can choose to buffer the write, returning `Ok(())`
11    /// after the complete slice has been written to a buffer, but before all
12    /// words have been sent via the serial interface. To make sure that
13    /// everything has been sent, call [`bflush`] after this function returns.
14    ///
15    /// [`bflush`]: #tymethod.bflush
16    fn bwrite_all(&mut self, buffer: &[Word]) -> Result<(), Self::Error>;
17
18    /// Block until the serial interface has sent all buffered words
19    fn bflush(&mut self) -> Result<(), Self::Error>;
20}
21
22/// Blocking serial write
23pub mod write {
24    /// Marker trait to opt into default blocking write implementation
25    ///
26    /// Implementers of [`serial::Write`] can implement this marker trait
27    /// for their type. Doing so will automatically provide the default
28    /// implementation of [`blocking::serial::Write`] for the type.
29    ///
30    /// [`serial::Write`]: ../../serial/trait.Write.html
31    /// [`blocking::serial::Write`]: ../trait.Write.html
32    pub trait Default<Word>: ::serial::Write<Word> {}
33
34    impl<S, Word> ::blocking::serial::Write<Word> for S
35    where
36        S: Default<Word>,
37        Word: Clone,
38    {
39        type Error = S::Error;
40
41        fn bwrite_all(&mut self, buffer: &[Word]) -> Result<(), Self::Error> {
42            for word in buffer {
43                block!(self.write(word.clone()))?;
44            }
45
46            Ok(())
47        }
48
49        fn bflush(&mut self) -> Result<(), Self::Error> {
50            block!(self.flush())?;
51            Ok(())
52        }
53    }
54}