optimize Element::write_to_inner() with CoW

This commit is contained in:
Astro 2017-08-12 02:13:32 +02:00
parent a01cf553ae
commit 26f03c27ed

View file

@ -6,6 +6,7 @@ use std::collections::{btree_map, BTreeMap};
use std::str; use std::str;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
use std::borrow::Cow;
use error::{Error, ErrorKind, Result}; use error::{Error, ErrorKind, Result};
@ -431,8 +432,8 @@ impl Element {
/// Like `write_to()` but without the `<?xml?>` prelude /// Like `write_to()` but without the `<?xml?>` prelude
pub fn write_to_inner<W: Write>(&self, writer: &mut W) -> Result<()> { pub fn write_to_inner<W: Write>(&self, writer: &mut W) -> Result<()> {
let name = match &self.prefix { let name = match &self.prefix {
&None => self.name.to_owned(), &None => Cow::Borrowed(&self.name),
&Some(ref prefix) => format!("{}:{}", prefix, self.name), &Some(ref prefix) => Cow::Owned(format!("{}:{}", prefix, self.name)),
}; };
write!(writer, "<{}", name)?; write!(writer, "<{}", name)?;