-
-
Notifications
You must be signed in to change notification settings - Fork 827
Allow serialising format_args! directly #786
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
Comments
Possible specialisations for ease of use could be:
|
Neat idea. The chrono impls could be optimized similar to this and I doubt there would be much performance left to gain after that, but it may make a difference in other cases. This ties in to #746 as well. |
@nox would something like |
Personally, I'd rather not limit this to just types with a |
@clarcharr This is the only way I can think of of using the Display trait while not making a breaking change. What do you suggest? |
Can you give an example of the boilerplate it would require? FormatArgs has a Display impl so |
Or do you just mean to support |
@dtolnay Actually it should probably be |
Oh wait, I wasn't aware that |
The type of boilerplate I was thinking of was that using wrapper types to manually implement |
Instead of basing this on Display, it may be a good opportunity to base it on a trait of our own that is able to provide both a human-readable string representation and a compact binary representation. This would make a lot of sense for chrono, IpAddr etc. The idea would be that JSON and YAML and TOML serialize as a string while Bincode serializes as bytes. |
I mean, I think that there's value in having a minimal display trait for strings as well. Because the value sent to One example off the top of my head is that an object containing dates might want to display them in a format other than ISO, whereas a serialised format would likely prefer ISO. |
That seems like a job for |
The default implementation collects the Display value into a String and then passes that to Serializer::serialize_str when the std or collections features are enabled, otherwise it unconditionally returns an error.
The default implementation collects the Display value into a String and then passes that to Serializer::serialize_str when the std or collections features are enabled, otherwise it unconditionally returns an error.
The default implementation collects the Display value into a String and then passes that to Serializer::serialize_str when the std or collections features are enabled, otherwise it unconditionally returns an error.
I filed #790 to follow up on the readable vs compact aspect of this in a separate change. |
The default implementation collects the Display value into a String and then passes that to Serializer::serialize_str when the std or collections features are enabled, otherwise it unconditionally returns an error.
Introduce Serializer::collect_str (fixes #786)
Fixed in #789. |
Uh oh!
There was an error while loading. Please reload this page.
I've seen a fair number of
Serialize
implementations that involve callingto_string
and I wonder if this extra allocation could be removed somehow. Take this example from chrono:I wonder if we could perhaps modify this to:
So that the serializer could call
write
itself directly to a buffer. For formats that don't require escaping characters, this could involve writing the data directly to the output stream, and for formats that do, this might involve writing to a temporary buffer (only allocated once) and then copying the data out with the escaped characters in it. Perhaps it could also default to callingformat
and thenserialize_str
if implementors choose to not specialise it.Not sure how much of a performance gain this would be, but it seems worth investigating imo.
The text was updated successfully, but these errors were encountered: