-
Notifications
You must be signed in to change notification settings - Fork 183
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
add format macro implementation #369
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
Could you change it to return a Result
instead of panicking if it runs out of space? We should avoid implicit panics as much as possible. It's true std's format!
panics on fmt errors, but these are super rare. Running out of space is more likely.
Also I wonder if we could let rust infer the string length instead of having to specify it manually.
Thanks!
Sure thing. Regarding inferring the capacity: This should be possible. However I'm not sure this is a good idea. If you want to bind the string to a new variable, you now have to specify the entire type just to be allowed to add the capacity, which adds quite a bit of noise: let foo: helpless::String<10> = format!(...); It pretty much comes down to what is the more common usage pattern. Or I could experiment with making it optional in the macro, so that the first parameter is a string -> infer, and if it's a number -> use capacity. That might actually be a more elegant solution. |
yeah that's true. Supporting both syntaxes sounds good to me! like |
That's another crate still ;) |
Seems like my phone's autocorrect was in a funny mood. I updated the PR. The macro now returns a The inferred capacity unfortunately did not work as planned. I had to use a semicolon as separator instead. struct Inferred {
name: String<10>,
}
let inf = Inferred {
name: format!("Hello {}", "World")?,
};
// Notice semicolon here: otherwise Rust would pick up 10 as the format string and fail.
let explicit = format!(10; "Foo {}", "bar")?; Hope this is ok too. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looking great! Thanks for the PR
0363c69
to
e4d61f1
Compare
e4d61f1
to
1e270a9
Compare
did a small fix, instead of making |
Oh, yeah that's much nicer. I figured there was only |
Motivation:
If you want to create a formatted
heapless::String
at the moment, it is necessary to create a mutable string and modify it:It's a bit nicer to have a
format!
macro that constructs theString
in-place without requiring an intermediate step, just as Rust's standard library offers.