Skip to content

Commit eab3759

Browse files
authored
Merge pull request #507 from MabezDev/eio-doc-fmt-trick
Document `write_fmt` length trick for slice
2 parents b4dfac9 + 21abf1d commit eab3759

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

embedded-io/src/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,17 @@ pub trait Write: ErrorType {
401401
/// If you are using [`WriteReady`] to avoid blocking, you should not use this function.
402402
/// `WriteReady::write_ready()` returning true only guarantees the first call to `write()` will
403403
/// not block, so this function may still block in subsequent calls.
404+
///
405+
/// Unlike [`Write::write`], the number of bytes written is not returned. However, in the case of
406+
/// writing to an `&mut [u8]` its possible to calculate the number of bytes written by subtracting
407+
/// the length of the slice after the write, from the initial length of the slice.
408+
///
409+
/// ```rust
410+
/// # use embedded_io::Write;
411+
/// let mut buf: &mut [u8] = &mut [0u8; 256];
412+
/// let start = buf.len();
413+
/// let len = write!(buf, "{}", "Test").and_then(|_| Ok(start - buf.len()));
414+
/// ```
404415
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> Result<(), WriteFmtError<Self::Error>> {
405416
// Create a shim which translates a Write to a fmt::Write and saves
406417
// off I/O errors. instead of discarding them

0 commit comments

Comments
 (0)