Add `PrettyPrinter::print` convenience method

Closes: #165
Approved by: whitequark
This commit is contained in:
Andrew Cann 2018-02-15 17:17:53 +08:00 committed by Homu
parent 1097ba3110
commit ec4c48af6c
1 changed files with 11 additions and 0 deletions

View File

@ -93,6 +93,17 @@ impl<'a, T: PrettyPrint> PrettyPrinter<'a, T> {
}
}
impl<'a, T: PrettyPrint + AsRef<[u8]>> PrettyPrinter<'a, T> {
/// Create a `PrettyPrinter` which prints the given object.
pub fn print(printable: &'a T) -> PrettyPrinter<'a, T> {
PrettyPrinter {
prefix: "",
buffer: printable,
phantom: PhantomData,
}
}
}
impl<'a, T: PrettyPrint> fmt::Display for PrettyPrinter<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
T::pretty_print(&self.buffer, f, &mut PrettyIndent::new(self.prefix))