Simplify trait bound declarations

This commit is contained in:
Dirkjan Ochtman 2022-09-01 09:34:59 +02:00
parent 737fcf2595
commit 056f241d1d
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!( proc_macro::TokenStream::from(quote!(
impl #generics ToXml for #ident #generics { impl #generics ToXml for #ident #generics {
fn serialize<W>(&self, serializer: &mut instant_xml::Serializer<W>) -> Result<(), instant_xml::Error> fn serialize<W: std::fmt::Write>(&self, serializer: &mut instant_xml::Serializer<W>) -> Result<(), instant_xml::Error> {
where
W: std::fmt::Write,
{
let _ = serializer.consume_field_context(); let _ = serializer.consume_field_context();
let mut field_context = instant_xml::FieldContext { let mut field_context = instant_xml::FieldContext {
name: #root_name, name: #root_name,

View File

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

View File

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