From 02c22b953207331d256e09410fc5bf60ff97a9be Mon Sep 17 00:00:00 2001 From: Scott Mabin Date: Mon, 18 Sep 2023 16:02:17 +0100 Subject: [PATCH] document write_fmt length trick for slice --- embedded-io/src/lib.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/embedded-io/src/lib.rs b/embedded-io/src/lib.rs index 1b91f7a7c..b2add2b7e 100644 --- a/embedded-io/src/lib.rs +++ b/embedded-io/src/lib.rs @@ -401,6 +401,15 @@ 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 + /// let start = buf.len(); + /// let len = write!(buf, "{}", DisplayType).and_then(|_| Ok(start - buf.len())); + /// ``` fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> Result<(), WriteFmtError> { // Create a shim which translates a Write to a fmt::Write and saves // off I/O errors. instead of discarding them