(&mut self, buf: &[u8])
| 46 | |
| 47 | impl Write for SerialBuffer { |
| 48 | fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> { |
| 49 | // Simply fill the buffer if we're not allowed to write to the out |
| 50 | // device. |
| 51 | if !self.write_out.load(Ordering::Acquire) { |
| 52 | self.fill_buffer(buf); |
| 53 | return Ok(buf.len()); |
| 54 | } |
| 55 | |
| 56 | // In case we're allowed to write to the out device, we flush the |
| 57 | // content of the buffer. |
| 58 | self.flush()?; |
| 59 | |
| 60 | // If after flushing the buffer, it's still not empty, that means |
| 61 | // only a subset of the bytes was written and we should fill the buffer |
| 62 | // with what's coming from the serial. |
| 63 | if !self.buffer.is_empty() { |
| 64 | self.fill_buffer(buf); |
| 65 | return Ok(buf.len()); |
| 66 | } |
| 67 | |
| 68 | // We reach this point if we're allowed to write to the out device |
| 69 | // and we know there's nothing left in the buffer. |
| 70 | let mut offset = 0; |
| 71 | loop { |
| 72 | match self.out.write(&buf[offset..]) { |
| 73 | Ok(written_bytes) => { |
| 74 | if written_bytes < buf.len() - offset { |
| 75 | offset += written_bytes; |
| 76 | continue; |
| 77 | } |
| 78 | } |
| 79 | Err(e) => { |
| 80 | if !matches!(e.kind(), std::io::ErrorKind::WouldBlock) { |
| 81 | return Err(e); |
| 82 | } |
| 83 | self.fill_buffer(&buf[offset..]); |
| 84 | } |
| 85 | } |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | // Make sure we flush anything that might have been written to the |
| 90 | // out device. |
| 91 | self.out.flush()?; |
| 92 | |
| 93 | Ok(buf.len()) |
| 94 | } |
| 95 | |
| 96 | // This function flushes the content of the buffer to the out device if |
| 97 | // it is allowed to, otherwise this is a no-op. |
no test coverage detected