usb-logger: avoid data loss at pipe wraparound

This commit is contained in:
Felix Lelchuk 2024-03-20 21:15:22 +01:00
parent 3845288ffb
commit 53ed4b8b2e

View File

@ -151,7 +151,17 @@ struct Writer<'d, const N: usize>(&'d Pipe<CS, N>);
impl<'d, const N: usize> core::fmt::Write for Writer<'d, N> {
fn write_str(&mut self, s: &str) -> Result<(), core::fmt::Error> {
let _ = self.0.try_write(s.as_bytes());
// The Pipe is implemented in such way that we cannot
// write across the wraparound discontinuity.
let b = s.as_bytes();
if let Ok(n) = self.0.try_write(b) {
if n < b.len() {
// We wrote some data but not all, attempt again
// as the reason might be a wraparound in the
// ring buffer, which resolves on second attempt.
let _ = self.0.try_write(&b[n..]);
}
}
Ok(())
}
}