Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document write_fmt length trick for slice #507

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions embedded-io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,17 @@ pub trait Write: ErrorType {
/// If you are using [`WriteReady`] to avoid blocking, you should not use this function.
/// `WriteReady::write_ready()` returning true only guarantees the first call to `write()` will
/// not block, so this function may still block in subsequent calls.
///
/// Unlike [`Write::write`], the number of bytes written is not returned. However, in the case of
/// writing to an `&mut [u8]` its possible to calculate the number of bytes written by subtracting
/// the length of the slice after the write, from the initial length of the slice.
///
/// ```rust
/// # use embedded_io::Write;
/// let mut buf: &mut [u8] = &mut [0u8; 256];
/// let start = buf.len();
MabezDev marked this conversation as resolved.
Show resolved Hide resolved
/// let len = write!(buf, "{}", "Test").and_then(|_| Ok(start - buf.len()));
/// ```
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> Result<(), WriteFmtError<Self::Error>> {
// Create a shim which translates a Write to a fmt::Write and saves
// off I/O errors. instead of discarding them
Expand Down