Browse Source

Add `PrettyPrinter::print` convenience method

Closes: #165
Approved by: whitequark
Andrew Cann 7 years ago
parent
commit
f1e5c73cae
1 changed files with 11 additions and 0 deletions
  1. 11 0
      src/wire/pretty_print.rs

+ 11 - 0
src/wire/pretty_print.rs

@@ -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))