From b47ac87546ce3607908d806f989d2a7804193e48 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Fri, 12 Jan 2024 22:31:40 -0800 Subject: Simplify the Rope::leaf function. --- src/rope.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/rope.rs b/src/rope.rs index c5378b0..e74dd4c 100644 --- a/src/rope.rs +++ b/src/rope.rs @@ -65,7 +65,7 @@ impl Rope { })) } - fn leaf(buf: Rc<[u8]>) -> Rope { + fn leaf(buf: &[u8]) -> Rope { if buf.len() > MAX_NODE_SIZE { panic!("buffer too long"); } @@ -75,11 +75,10 @@ impl Rope { lines += 1; } } - let n = buf.len(); Rope(Node::Leaf(Leaf { - unsafe_buf: Some(buf), + unsafe_buf: Some(Rc::from(buf)), start: 0, - end: u16::try_from(n).expect("buffer too long"), + end: u16::try_from(buf.len()).expect("buffer too long"), lines, })) } @@ -104,15 +103,15 @@ impl Rope { let mut buf = Vec::with_capacity(self.len() + other.len()); buf.extend_from_slice(l.bytes()); buf.extend_from_slice(r.bytes()); - return Insert::Node(Rope::leaf(Rc::from(buf))); + return Insert::Node(Rope::leaf(&buf)); } if self.len() < MAX_NODE_SIZE / 2 || other.len() < MAX_NODE_SIZE / 2 { let mut buf = Vec::with_capacity(self.len() + other.len()); buf.extend_from_slice(l.bytes()); buf.extend_from_slice(r.bytes()); return Insert::Split( - Rope::leaf(Rc::from(&buf[..buf.len() / 2])), - Rope::leaf(Rc::from(&buf[buf.len() / 2..])), + Rope::leaf(&buf[..buf.len() / 2]), + Rope::leaf(&buf[buf.len() / 2..]), ); } return Insert::Split(self.clone(), other.clone()); @@ -215,14 +214,14 @@ impl Rope { if n == 0 { break; } - rope = rope.concat(&Rope::leaf(Rc::from(&buf[..n]))); + rope = rope.concat(&Rope::leaf(&buf[..n])); } Ok(rope) } pub fn new(buf: &[u8]) -> Rope { if buf.len() <= MAX_NODE_SIZE { - return Rope::leaf(Rc::from(buf)); + return Rope::leaf(buf); } let mut buf = buf; Rope::read(&mut buf).expect("reading from a slice should never fail") -- cgit v1.3.1