-
Notifications
You must be signed in to change notification settings - Fork 214
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
Updating write functions to return length of written bytes #502
Conversation
This breaks compatibility with An alternative to this, which I have used in the past before even embedded-io existed is to use a wrapper type which counts the number of bytes written to the inner buffer, which you can then retrieve after you're done formatting. Perhaps something like this could be added to this crate to avoid people rolling their own. |
I guess a valid question would be whether or not we're trying to be ane exact replacement. If so, then I agree that we should keep the same function signature.
Yeah, which is essentially the embedded equivalent of Edit: To answer my own question, there's already some documented differences: https://github.com/rust-embedded/embedded-hal/tree/master/embedded-io#differences-with-stdio |
Using the Here's an example where a closure takes an let mut cursor = Cursor::new(buf);
write!(cursor, "{:?}", error)?;
Ok::<_, embedded_io::WriteFmtError<core::convert::Infallible>>(cursor.position()) |
Good question! I'm not sure of the long-term goal of embedded-io. I guess @Dirbaio has an idea.
I think the main difference here is that those are differences because of limitations in |
The
And the
So code using the |
The let mut bytes = [0; 1024];
let mut w = &mut bytes[..];
let len = w.len();
write!(w, "{:?}", 1234).unwrap();
let n = len - w.len();
println!("{}", n); // prints 4
How have you implemented I'm not sure whether deviating from std::io is worth it on this. Getting the written length is already possible (with either the snippet above, or Cursor). I agree it's somewhat verbose, but so is std::io. |
Ah, this indeed solves my use-case quite well with a single extra function call, which is even easier than the Cursor-based approach. Thanks for the info!
Apparently not because I got the exact same error when using your snippet as: let start = buf.len();
write!(buf, "{}", MyType)?;
Ok(start - buf.len()) However, oddly enough doing the following fixed it. I have a suspicion its because Rustc may be being arbitrarily restrictive on inferring Closure return types: let start = buf.len();
write!(buf, "{}", MyType).and_then(|_| Ok(start - buf.len()) For context, the exact code I'm referring to is here: https://github.com/quartiq/miniconf/pull/134/files#diff-8ce4ae904c14212d6bed3296e5ced8732145b011aa19e6cb2fa5c614698d6f4cR492-R495 - the In any case, this does indeed appear possible with the current API (albeit quite unintuitive to the user). I have no issue with closing this PR if we determine that we want to maintain exact API compatibility, although as @jannic pointed out, the return type isn't currently used anyways. |
Could some of you document this problem and solution in the method or somewhere else in |
I opened a PR here: #507 |
This PR updates the
embedded_io::Write
trait to return the number of bytes written for thewrite_fmt
function. It seems the length was originally omitted out of simplicity/lack of demand.However, during my usage, I noted that without this, using a byte slice with
write!()
doesn't provide a result to indicate where the written object ends: