浏览代码

Add `PrettyPrinter::print` convenience method

Closes: #165
Approved by: whitequark
Andrew Cann 7 年之前
父节点
当前提交
f1e5c73cae
共有 1 个文件被更改,包括 11 次插入0 次删除
  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))