From ea587c23c6a6079bb0b7714e33775b067df3a129 Mon Sep 17 00:00:00 2001 From: Hana Date: Tue, 10 Dec 2024 19:44:55 +0800 Subject: [PATCH] perf: toString --- src/helpers.rs | 3 +-- src/rope.rs | 12 ++++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index 6f27db0d..9fc7b296 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,7 +1,6 @@ use std::{ borrow::{BorrowMut, Cow}, cell::{OnceCell, RefCell}, - fmt::Display, marker::PhantomData, ops::Range, }; @@ -1287,7 +1286,7 @@ pub fn stream_and_get_source_and_map<'a, S: StreamChunks>( (generated_info, map) } -pub trait SourceText<'a>: Default + Clone + Display { +pub trait SourceText<'a>: Default + Clone + ToString { fn split_into_lines(&self) -> impl Iterator; fn len(&self) -> usize; fn ends_with(&self, value: &str) -> bool; diff --git a/src/rope.rs b/src/rope.rs index 8549c688..2da1d23a 100644 --- a/src/rope.rs +++ b/src/rope.rs @@ -2,7 +2,6 @@ use std::{ borrow::Cow, cell::RefCell, collections::VecDeque, - fmt::Display, hash::Hash, ops::{Bound, RangeBounds}, rc::Rc, @@ -415,15 +414,16 @@ impl Default for Rope<'_> { } } -impl Display for Rope<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl ToString for Rope<'_> { + fn to_string(&self) -> String { match &self.0 { - Repr::Simple(s) => write!(f, "{}", s), + Repr::Simple(s) => s.to_string(), Repr::Complex(data) => { + let mut s = String::with_capacity(self.len()); for (chunk, _) in data.iter() { - write!(f, "{}", chunk)?; + s.push_str(chunk); } - Ok(()) + s } } }