Skip to content

Commit 20886ec

Browse files
committed
feat: to_cow_string for rope
1 parent 045714a commit 20886ec

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/cached_source.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl<T> CachedSource<T> {
9494

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

100100
fn rope(&self) -> Rope<'_> {

src/rope.rs

+14
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,20 @@ impl<'a> Rope<'a> {
461461
}
462462
}
463463
}
464+
465+
/// Converts the rope to Cow<str>.
466+
pub fn to_cow_string(&self) -> Cow<str> {
467+
match &self.repr {
468+
Repr::Light(s) => Cow::Borrowed(s),
469+
Repr::Full(data) => {
470+
let mut s = String::with_capacity(self.len());
471+
for (chunk, _) in data.iter() {
472+
s.push_str(chunk);
473+
}
474+
Cow::Owned(s)
475+
}
476+
}
477+
}
464478
}
465479

466480
impl Hash for Rope<'_> {

0 commit comments

Comments
 (0)