From 2f3dd72d90e559d601e984747e8f6cb9ad372669 Mon Sep 17 00:00:00 2001 From: Emii Tatsuo Date: Mon, 30 Nov 2020 00:52:08 -0500 Subject: [PATCH] Add AsRef and AsMut<[Node]> to builder --- gemtext/src/lib.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/gemtext/src/lib.rs b/gemtext/src/lib.rs index 71ab545..f9cc830 100644 --- a/gemtext/src/lib.rs +++ b/gemtext/src/lib.rs @@ -61,7 +61,7 @@ impl ToString for Builder { fn to_string(&self) -> String { let len: usize = self.nodes.iter().map(Node::estimate_len).sum(); // sum up node lengths let mut bytes = Vec::with_capacity(len + self.nodes.len()); // add in inter-node newlines - render(&self.nodes, &mut bytes).unwrap(); // Writing to a string shouldn't produce errors + render(self, &mut bytes).unwrap(); // Writing to a string shouldn't produce errors unsafe { // This is safe because bytes is composed of Strings. We could have this as @@ -73,6 +73,20 @@ impl ToString for Builder { } } +impl AsRef<[Node]> for Builder { + /// Get a reference to the internal node list of this builder + fn as_ref(&self) -> &[Node] { + self.nodes.as_ref() + } +} + +impl AsMut<[Node]> for Builder { + /// Get a mutable reference to the internal node list of this builder + fn as_mut(&mut self) -> &mut [Node] { + self.nodes.as_mut() + } +} + /// Render a set of nodes as a document to a writer. pub fn render(nodes: impl AsRef<[Node]>, out: &mut impl Write) -> io::Result<()> { use Node::*;