Skip to content

Commit

Permalink
feat: to_cow_string for rope
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Dec 16, 2024
1 parent 045714a commit 20886ec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cached_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl<T> CachedSource<T> {

impl<T: Source + Hash + PartialEq + Eq + 'static> Source for CachedSource<T> {
fn source(&self) -> Cow<str> {
Cow::Owned(self.get_rope().to_string())
self.get_rope().to_cow_string()
}

fn rope(&self) -> Rope<'_> {
Expand Down
14 changes: 14 additions & 0 deletions src/rope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,20 @@ impl<'a> Rope<'a> {
}
}
}

/// Converts the rope to Cow<str>.
pub fn to_cow_string(&self) -> Cow<str> {
match &self.repr {
Repr::Light(s) => Cow::Borrowed(s),
Repr::Full(data) => {
let mut s = String::with_capacity(self.len());
for (chunk, _) in data.iter() {
s.push_str(chunk);
}
Cow::Owned(s)
}
}
}
}

impl Hash for Rope<'_> {
Expand Down

0 comments on commit 20886ec

Please sign in to comment.