Skip to content

Commit 6e46ac9

Browse files
committed
refactor rope
1 parent 9eb87d6 commit 6e46ac9

File tree

9 files changed

+144
-249
lines changed

9 files changed

+144
-249
lines changed

src/cached_source.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
},
1414
object_pool::ObjectPool,
1515
source::SourceValue,
16-
BoxSource, MapOptions, Rope, Source, SourceExt, SourceMap,
16+
BoxSource, MapOptions, Source, SourceExt, SourceMap,
1717
};
1818

1919
#[derive(Default)]
@@ -82,11 +82,10 @@ impl CachedSource {
8282

8383
fn get_or_init_chunks(&self) -> &[&str] {
8484
self.cache.chunks.get_or_init(|| {
85-
let rope = self.inner.rope();
86-
let chunks = match rope {
87-
Rope::Light(s) => vec![s],
88-
Rope::Full(iter) => iter.collect::<Vec<_>>(),
89-
};
85+
let mut chunks = Vec::new();
86+
self.inner.rope(&mut |chunk| {
87+
chunks.push(chunk);
88+
});
9089
#[allow(unsafe_code)]
9190
// SAFETY: CachedSource guarantees that the underlying source outlives the cache,
9291
// so transmuting Vec<&str> to Vec<&'static str> is safe in this context.
@@ -108,9 +107,9 @@ impl Source for CachedSource {
108107
SourceValue::String(Cow::Owned(string))
109108
}
110109

111-
fn rope(&self) -> Rope {
110+
fn rope<'a>(&'a self, on_chunk: &mut dyn FnMut(&'a str)) {
112111
let chunks = self.get_or_init_chunks();
113-
Rope::Full(Box::new(chunks.iter().cloned()))
112+
chunks.iter().for_each(|chunk| on_chunk(chunk));
114113
}
115114

116115
fn buffer(&self) -> Cow<[u8]> {

src/concat_source.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
linear_map::LinearMap,
1313
object_pool::ObjectPool,
1414
source::{Mapping, OriginalLocation},
15-
BoxSource, MapOptions, RawStringSource, Rope, Source, SourceExt, SourceMap,
15+
BoxSource, MapOptions, RawStringSource, Source, SourceExt, SourceMap,
1616
SourceValue,
1717
};
1818

@@ -169,31 +169,19 @@ impl Source for ConcatSource {
169169
}
170170

171171
let mut string = String::with_capacity(self.size());
172-
for child in children {
173-
match child.rope() {
174-
Rope::Light(s) => string.push_str(s),
175-
Rope::Full(chunks) => {
176-
for chunk in chunks {
177-
string.push_str(chunk);
178-
}
179-
}
180-
}
181-
}
172+
children.iter().for_each(|child| {
173+
child.rope(&mut |chunk| {
174+
string.push_str(chunk);
175+
});
176+
});
182177
SourceValue::String(Cow::Owned(string))
183178
}
184179

185-
fn rope(&self) -> Rope {
180+
fn rope<'a>(&'a self, on_chunk: &mut dyn FnMut(&'a str)) {
186181
let children = self.optimized_children();
187-
if children.len() == 1 {
188-
children[0].rope()
189-
} else {
190-
Rope::Full(Box::new(children.iter().flat_map(
191-
|child| match child.rope() {
192-
Rope::Light(s) => Box::new(std::iter::once(s)),
193-
Rope::Full(iter) => iter,
194-
},
195-
)))
196-
}
182+
children.iter().for_each(|child| {
183+
child.rope(on_chunk);
184+
});
197185
}
198186

199187
fn buffer(&self) -> Cow<[u8]> {

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub use original_source::OriginalSource;
2323
pub use raw_source::{RawBufferSource, RawStringSource};
2424
pub use replace_source::{ReplaceSource, ReplacementEnforce};
2525
pub use source::{
26-
BoxSource, MapOptions, Mapping, OriginalLocation, Rope, Source, SourceExt,
26+
BoxSource, MapOptions, Mapping, OriginalLocation, Source, SourceExt,
2727
SourceMap, SourceValue,
2828
};
2929
pub use source_map_source::{

src/original_source.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
},
1212
object_pool::ObjectPool,
1313
source::{Mapping, OriginalLocation},
14-
MapOptions, Rope, Source, SourceMap, SourceValue,
14+
MapOptions, Source, SourceMap, SourceValue,
1515
};
1616

1717
/// Represents source code, it will create source map for the source code,
@@ -56,8 +56,8 @@ impl Source for OriginalSource {
5656
SourceValue::String(Cow::Borrowed(&self.value))
5757
}
5858

59-
fn rope(&self) -> Rope {
60-
Rope::Light(self.value.as_ref())
59+
fn rope<'a>(&'a self, on_chunk: &mut dyn FnMut(&'a str)) {
60+
on_chunk(self.value.as_ref())
6161
}
6262

6363
fn buffer(&self) -> Cow<[u8]> {

src/raw_source.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
GeneratedInfo, StreamChunks,
1111
},
1212
object_pool::ObjectPool,
13-
MapOptions, Rope, Source, SourceMap, SourceValue,
13+
MapOptions, Source, SourceMap, SourceValue,
1414
};
1515

1616
/// A string variant of [RawStringSource].
@@ -64,8 +64,8 @@ impl Source for RawStringSource {
6464
SourceValue::String(Cow::Borrowed(&self.0))
6565
}
6666

67-
fn rope(&self) -> Rope {
68-
Rope::Light(self.0.as_ref())
67+
fn rope<'a>(&'a self, on_chunk: &mut dyn FnMut(&'a str)) {
68+
on_chunk(self.0.as_ref())
6969
}
7070

7171
fn buffer(&self) -> Cow<[u8]> {
@@ -210,8 +210,8 @@ impl Source for RawBufferSource {
210210
SourceValue::Buffer(Cow::Borrowed(&self.value))
211211
}
212212

213-
fn rope(&self) -> Rope {
214-
Rope::Light(self.get_or_init_value_as_string())
213+
fn rope<'a>(&'a self, on_chunk: &mut dyn FnMut(&'a str)) {
214+
on_chunk(self.get_or_init_value_as_string())
215215
}
216216

217217
fn buffer(&self) -> Cow<[u8]> {

0 commit comments

Comments
 (0)