diff options
| author | Rose Hogenson <rosehogenson@posteo.net> | 2024-01-12 22:31:40 -0800 |
|---|---|---|
| committer | Rose Hogenson <rosehogenson@posteo.net> | 2024-01-12 22:31:40 -0800 |
| commit | b47ac87546ce3607908d806f989d2a7804193e48 (patch) | |
| tree | 01b391598f1266c2fe354f62754905a2c7fdd96f /src/rope.rs | |
| parent | 7078c6cd1d0fd008946edb3b0a964b8e83a83bc1 (diff) | |
| download | editor-b47ac87546ce3607908d806f989d2a7804193e48.tar.zst | |
Simplify the Rope::leaf function.
Diffstat (limited to 'src/rope.rs')
| -rw-r--r-- | src/rope.rs | 17 |
1 files 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") |
