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

Introduce Serializer::collect_str (fixes #786) #789

Merged
merged 6 commits into from Mar 6, 2017
Merged

Conversation

nox
Copy link
Contributor

@nox nox commented Feb 26, 2017

No description provided.

@clarfonthey
Copy link
Contributor

For no_std, wouldn't it be best to have a reasonably sized buffer to cover cases where strings are small? Could we potentially also cover this in the std case?

@dtolnay
Copy link
Member

dtolnay commented Feb 26, 2017

We can optimize this with a statically allocated buffer in a separate commit. This API looks reasonable to me. @nox could you also implement the serde_json side of this?

@clarfonthey
Copy link
Contributor

My main concern is that the no_std case will just return a runtime error instead of doing anything reasonable. I'd rather return at least an error that points to this method than something cryptic.

@nox
Copy link
Contributor Author

nox commented Feb 27, 2017

@dtolnay Done.

@nox
Copy link
Contributor Author

nox commented Feb 27, 2017

@clarcharr I will change the error message tomorrow. Any suggestions?

@clarfonthey
Copy link
Contributor

@nox Perhaps "collect_str is currently unimplemented for no_std builds" ?

@nox
Copy link
Contributor Author

nox commented Feb 27, 2017

I changed the error message and used unwrap for the result returned by write!, given it's documented that Display::fmt should never return spurious errors.

where T: Display,
{
let mut string = String::new();
write!(string, "{}", value).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any advantages of this over value.to_string()? I guess in case there is a specialized ToString impl to make this default implementation behave consistently with a serializer that overrides collect_str with a more efficient Display-based implementation? I think people deserve what they get if they implement ToString and Display in a way that is not consistent with each other.

ToString may be faster rust-lang/rust#18404 (not that I expect this method to be used with T=str).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to use ToString, but unfortunately, I have to hate it for now.

/// The default implementation serializes the given value as a string with
/// `ToString::to_string`.
#[cfg(any(feature = "std", feature = "collections"))]
fn collect_str<T>(self, value: &T) -> Result<Self::Ok, Self::Error>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

T can be ?Sized.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

@clarfonthey
Copy link
Contributor

Would it be reasonable to add a token to serde_test::Token for this?

@dtolnay
Copy link
Member

dtolnay commented Feb 28, 2017

I don't think so, semantically it is no different from a string.

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.
@dtolnay dtolnay merged commit 17bc40e into serde-rs:master Mar 6, 2017
@shepmaster
Copy link
Contributor

Very cool idea — I was just looking for something like this! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants