Simplify trait bound declarations

This commit is contained in:
Dirkjan Ochtman 2022-09-01 09:34:59 +02:00
parent 891fc81e9e
commit c62bf5632d
3 changed files with 17 additions and 52 deletions

View File

@ -136,10 +136,7 @@ pub fn to_xml(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
proc_macro::TokenStream::from(quote!(
impl #generics ToXml for #ident #generics {
fn serialize<W>(&self, serializer: &mut instant_xml::Serializer<W>) -> Result<(), instant_xml::Error>
where
W: std::fmt::Write,
{
fn serialize<W: std::fmt::Write>(&self, serializer: &mut instant_xml::Serializer<W>) -> Result<(), instant_xml::Error> {
let _ = serializer.consume_field_context();
let mut field_context = instant_xml::FieldContext {
name: #root_name,

View File

@ -81,10 +81,10 @@ where
macro_rules! to_xml_for_number {
($typ:ty) => {
impl ToXml for $typ {
fn serialize<W>(&self, serializer: &mut Serializer<W>) -> Result<(), Error>
where
W: fmt::Write,
{
fn serialize<W: fmt::Write>(
&self,
serializer: &mut Serializer<W>,
) -> Result<(), Error> {
DisplayToXml(self).serialize(serializer)
}
}
@ -322,10 +322,7 @@ to_xml_for_number!(f32);
to_xml_for_number!(f64);
impl ToXml for bool {
fn serialize<W>(&self, serializer: &mut Serializer<W>) -> Result<(), Error>
where
W: fmt::Write,
{
fn serialize<W: fmt::Write>(&self, serializer: &mut Serializer<W>) -> Result<(), Error> {
let value = match self {
true => "true",
false => "false",
@ -336,50 +333,32 @@ impl ToXml for bool {
}
impl ToXml for String {
fn serialize<W>(&self, serializer: &mut Serializer<W>) -> Result<(), Error>
where
W: fmt::Write,
{
fn serialize<W: fmt::Write>(&self, serializer: &mut Serializer<W>) -> Result<(), Error> {
DisplayToXml(&escape(self)?).serialize(serializer)
}
}
impl ToXml for char {
fn serialize<W>(&self, serializer: &mut Serializer<W>) -> Result<(), Error>
where
W: fmt::Write,
{
fn serialize<W: fmt::Write>(&self, serializer: &mut Serializer<W>) -> Result<(), Error> {
let mut tmp = [0u8; 4];
DisplayToXml(&escape(&*self.encode_utf8(&mut tmp))?).serialize(serializer)
}
}
impl ToXml for &str {
fn serialize<W>(&self, serializer: &mut Serializer<W>) -> Result<(), Error>
where
W: fmt::Write,
{
fn serialize<W: fmt::Write>(&self, serializer: &mut Serializer<W>) -> Result<(), Error> {
DisplayToXml(&escape(self)?).serialize(serializer)
}
}
impl ToXml for Cow<'_, str> {
fn serialize<W>(&self, serializer: &mut Serializer<W>) -> Result<(), Error>
where
W: fmt::Write,
{
fn serialize<W: fmt::Write>(&self, serializer: &mut Serializer<W>) -> Result<(), Error> {
DisplayToXml(&escape(self)?).serialize(serializer)
}
}
impl<T> ToXml for Option<T>
where
T: ToXml,
{
fn serialize<W>(&self, serializer: &mut Serializer<W>) -> Result<(), Error>
where
W: fmt::Write,
{
impl<T: ToXml> ToXml for Option<T> {
fn serialize<W: fmt::Write>(&self, serializer: &mut Serializer<W>) -> Result<(), Error> {
match self {
Some(v) => v.serialize(serializer),
None => Ok(()),

View File

@ -34,15 +34,10 @@ pub trait ToXml {
Ok(output)
}
fn serialize<W>(&self, serializer: &mut Serializer<W>) -> Result<(), Error>
where
W: fmt::Write;
fn serialize<W: fmt::Write>(&self, serializer: &mut Serializer<W>) -> Result<(), Error>;
}
pub struct Serializer<'xml, W>
where
W: fmt::Write,
{
pub struct Serializer<'xml, W: fmt::Write> {
// For parent namespaces the key is the namespace and the value is the prefix. We are adding to map
// only if the namespaces do not exist, if it does exist then we are using an already defined parent prefix.
#[doc(hidden)]
@ -56,7 +51,7 @@ where
next_field_context: Option<FieldContext<'xml>>,
}
impl<'xml, W: std::fmt::Write> Serializer<'xml, W> {
impl<'xml, W: fmt::Write> Serializer<'xml, W> {
pub fn new(output: &'xml mut W) -> Self {
Self {
parent_namespaces: HashMap::new(),
@ -74,20 +69,14 @@ impl<'xml, W: std::fmt::Write> Serializer<'xml, W> {
Ok(())
}
pub fn add_attribute_key<T>(&mut self, attr_key: &T) -> Result<(), Error>
where
T: fmt::Display,
{
pub fn add_attribute_key(&mut self, attr_key: &impl fmt::Display) -> Result<(), Error> {
self.current_attributes.push(' ');
write!(self.current_attributes, "{}", attr_key)?;
self.current_attributes.push('=');
Ok(())
}
pub fn add_attribute_value<T>(&mut self, attr_value: &T) -> Result<(), Error>
where
T: fmt::Display,
{
pub fn add_attribute_value(&mut self, attr_value: &impl fmt::Display) -> Result<(), Error> {
self.current_attributes.push('"');
write!(self.current_attributes, "{}", attr_value)?;
self.current_attributes.push('"');