diff options
| author | Felix Lelchuk <[email protected]> | 2024-03-20 21:15:22 +0100 |
|---|---|---|
| committer | Felix Lelchuk <[email protected]> | 2024-03-20 21:15:22 +0100 |
| commit | 53ed4b8b2e79ee41692132793aa7910dd972e524 (patch) | |
| tree | e40390e0e80f0f473e9bebc7d5b3cfbb908accde /embassy-usb-logger | |
| parent | 3845288ffb0418cc375430dd366a1a49ccee6f5a (diff) | |
usb-logger: avoid data loss at pipe wraparound
Diffstat (limited to 'embassy-usb-logger')
| -rw-r--r-- | embassy-usb-logger/src/lib.rs | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/embassy-usb-logger/src/lib.rs b/embassy-usb-logger/src/lib.rs index da5ff0f36..6f4836d21 100644 --- a/embassy-usb-logger/src/lib.rs +++ b/embassy-usb-logger/src/lib.rs | |||
| @@ -151,7 +151,17 @@ struct Writer<'d, const N: usize>(&'d Pipe<CS, N>); | |||
| 151 | 151 | ||
| 152 | impl<'d, const N: usize> core::fmt::Write for Writer<'d, N> { | 152 | impl<'d, const N: usize> core::fmt::Write for Writer<'d, N> { |
| 153 | fn write_str(&mut self, s: &str) -> Result<(), core::fmt::Error> { | 153 | fn write_str(&mut self, s: &str) -> Result<(), core::fmt::Error> { |
| 154 | let _ = self.0.try_write(s.as_bytes()); | 154 | // The Pipe is implemented in such way that we cannot |
| 155 | // write across the wraparound discontinuity. | ||
| 156 | let b = s.as_bytes(); | ||
| 157 | if let Ok(n) = self.0.try_write(b) { | ||
| 158 | if n < b.len() { | ||
| 159 | // We wrote some data but not all, attempt again | ||
| 160 | // as the reason might be a wraparound in the | ||
| 161 | // ring buffer, which resolves on second attempt. | ||
| 162 | let _ = self.0.try_write(&b[n..]); | ||
| 163 | } | ||
| 164 | } | ||
| 155 | Ok(()) | 165 | Ok(()) |
| 156 | } | 166 | } |
| 157 | } | 167 | } |
