- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 813
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
impl<'a> Serialize for fmt::Arguments<'a> #1319
Comments
Well... We could add an impl of DISCLAIMER: all the code below is entirely untested and probably will not compile without some adjustments impl<S: Serializer> std::fmt::Write for S {
#[inline]
fn write_str(&mut self, s: &str) -> std::fmt::Result {
self.serialize_str(s).map_err(|_| std::fmt::Error)
}
} Although that would be a breaking change, because someone might have implemented We can still do this by adding a wrapper type for impl<'a> Serialize for fmt::Arguments<'a> {
fn serialize<S: Serializer>(&self, s: &mut S) -> Result<(), S::Error> {
struct Wrapper<'b, S: Serializer>(&'b mut S, Option<S::Error>);
impl<'b, S: Serializer> std::fmt::Write for Wrapper<'b, S> {
#[inline]
fn write_str(&mut self, s: &str) -> std::fmt::Result {
self.0.serialize_str(s).map_err(|err| {
assert!(self.1.is_none());
self.1 = Some(err);
std::fmt::Error
})
}
}
let mut wrapper = Wrapper(s);
match wrapper.write_fmt(*self) {
Ok(()) => Ok(()),
Err(std::fmt::Error) => Ok(wrapper.1.unwrap()),
}
}
} Unfortunately this means you get a bunch of strings in the output, not sure how useful that is. What's the expected output format? A sequence of strings? |
Thanks for response. I don't think serializaing this value to series of
In my use-case ( I'm toying with some API re-designs where publicly accepted types would be In ideal case |
Thanks! |
Would it make sense to impl<'a> Serialize for fmt::Arguments<'a> ?
It's a very handy type, and available in
core
library. Though I guess for best support, theSerializer
would have to get aserialize_fmt
method, so it is possible to write it out without allocating temporary strings, etc. That would probably be a breaking change, though maybe an implementation that serializes into temporary, and callsserialize_str
or something would do.The text was updated successfully, but these errors were encountered: