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

write_fmt return number of bytes written #63614

Closed
MabezDev opened this issue Aug 15, 2019 · 4 comments
Closed

write_fmt return number of bytes written #63614

MabezDev opened this issue Aug 15, 2019 · 4 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. rust-2-breakage-wishlist In the wishlist of breaking changes that requires rust 2.0 T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@MabezDev
Copy link
Contributor

The current write_fmt implementation has a return type of Result<()> is there any technical reason it couldn't return the number of bytes written? Similar to sprintf in C.

An example use case in Smoltcp:

socket.send(|mut buffer| {
    let bytes_written = write!(buffer, "Http Error {:?}", e).unwrap();
    (bytes_written, ())
}).unwrap();

The return type of the closure requires indicates the number of bytes used such that smoltcp can flush those bytes to the wire.

@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. rust-2-breakage-wishlist In the wishlist of breaking changes that requires rust 2.0 T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Aug 15, 2019
@jonas-schievink
Copy link
Contributor

I don't think there's a technical reason for it, but changing it would be a breaking change, so it's not going to happen.

@MabezDev
Copy link
Contributor Author

Fair enough. Is there anyway to use the formatter and get the number of bytes it will create? I really want to avoid hand rolling formatting if possible.

@jonas-schievink
Copy link
Contributor

The only way I can think of is wrapping the formatter and keeping your own count

@MabezDev
Copy link
Contributor Author

It's a bit awkward, but it works. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. rust-2-breakage-wishlist In the wishlist of breaking changes that requires rust 2.0 T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants