Skip to content

Commit

Permalink
usb-logger: avoid data loss at pipe wraparound
Browse files Browse the repository at this point in the history
  • Loading branch information
flelchuk committed Mar 20, 2024
1 parent 3845288 commit 53ed4b8
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion embassy-usb-logger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
Expand Down

0 comments on commit 53ed4b8

Please sign in to comment.