diff options
Diffstat (limited to 'vendor/handlebars/src/output.rs')
-rw-r--r-- | vendor/handlebars/src/output.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/vendor/handlebars/src/output.rs b/vendor/handlebars/src/output.rs index 67e62b849..12075b365 100644 --- a/vendor/handlebars/src/output.rs +++ b/vendor/handlebars/src/output.rs @@ -6,6 +6,19 @@ use std::string::FromUtf8Error; /// Handlebars uses this trait to define rendered output. pub trait Output { fn write(&mut self, seg: &str) -> Result<(), IOError>; + + /// Designed to be used with `write!` macro. + /// for backward compatibility and to avoid breakage the default implementation + /// uses `format!` this may be not what you want. + fn write_fmt(&mut self, args: std::fmt::Arguments<'_>) -> Result<(), IOError> { + // Check if there is nothing to format to avoid allocation on case like + // write!(out, "hey")?; + if let Some(content) = args.as_str() { + self.write(content) + } else { + self.write(&std::fmt::format(args)) + } + } } pub struct WriteOutput<W: Write> { @@ -16,6 +29,10 @@ impl<W: Write> Output for WriteOutput<W> { fn write(&mut self, seg: &str) -> Result<(), IOError> { self.write.write_all(seg.as_bytes()) } + + fn write_fmt(&mut self, args: std::fmt::Arguments<'_>) -> Result<(), IOError> { + self.write.write_fmt(args) + } } impl<W: Write> WriteOutput<W> { @@ -33,6 +50,10 @@ impl Output for StringOutput { self.buf.extend_from_slice(seg.as_bytes()); Ok(()) } + + fn write_fmt(&mut self, args: std::fmt::Arguments<'_>) -> Result<(), IOError> { + self.buf.write_fmt(args) + } } impl StringOutput { |