11use std:: {
22 borrow:: Cow ,
3+ cell:: OnceCell ,
34 hash:: { Hash , Hasher } ,
45 sync:: { Arc , OnceLock } ,
56} ;
@@ -9,10 +10,9 @@ use rustc_hash::FxHasher;
910use crate :: {
1011 helpers:: {
1112 stream_and_get_source_and_map, stream_chunks_of_raw_source,
12- stream_chunks_of_source_map, StreamChunks ,
13+ stream_chunks_of_source_map, Chunks , GeneratedInfo , StreamChunks ,
1314 } ,
1415 object_pool:: ObjectPool ,
15- rope:: Rope ,
1616 source:: SourceValue ,
1717 BoxSource , MapOptions , Source , SourceExt , SourceMap ,
1818} ;
@@ -86,10 +86,6 @@ impl Source for CachedSource {
8686 self . inner . source ( )
8787 }
8888
89- fn rope ( & self ) -> Rope < ' _ > {
90- self . inner . rope ( )
91- }
92-
9389 fn buffer ( & self ) -> Cow < [ u8 ] > {
9490 let mut buffer = vec ! [ ] ;
9591 self . to_writer ( & mut buffer) . unwrap ( ) ;
@@ -125,44 +121,68 @@ impl Source for CachedSource {
125121 }
126122}
127123
128- impl StreamChunks for CachedSource {
129- fn stream_chunks < ' a > (
124+ struct CachedSourceChunks < ' source > {
125+ chunks : Box < dyn Chunks + ' source > ,
126+ cache : Arc < CachedData > ,
127+ inner : & ' source dyn Source ,
128+ source : OnceCell < Cow < ' source , str > > ,
129+ }
130+
131+ impl < ' a > CachedSourceChunks < ' a > {
132+ fn new ( cache_source : & ' a CachedSource ) -> Self {
133+ Self {
134+ chunks : cache_source. inner . stream_chunks ( ) ,
135+ cache : cache_source. cache . clone ( ) ,
136+ inner : & cache_source. inner ,
137+ source : OnceCell :: new ( ) ,
138+ }
139+ }
140+ }
141+
142+ impl Chunks for CachedSourceChunks < ' _ > {
143+ fn stream < ' a > (
130144 & ' a self ,
131145 object_pool : & ' a ObjectPool ,
132146 options : & MapOptions ,
133147 on_chunk : crate :: helpers:: OnChunk < ' _ , ' a > ,
134148 on_source : crate :: helpers:: OnSource < ' _ , ' a > ,
135149 on_name : crate :: helpers:: OnName < ' _ , ' a > ,
136- ) -> crate :: helpers :: GeneratedInfo {
150+ ) -> GeneratedInfo {
137151 let cell = if options. columns {
138152 & self . cache . columns_map
139153 } else {
140154 & self . cache . line_only_map
141155 } ;
142156 match cell. get ( ) {
143157 Some ( map) => {
144- let source = self . rope ( ) ;
158+ let source = self
159+ . source
160+ . get_or_init ( || self . inner . source ( ) . into_string_lossy ( ) ) ;
145161 if let Some ( map) = map {
146162 stream_chunks_of_source_map (
147163 options,
148164 object_pool,
149- source,
165+ source. as_ref ( ) ,
150166 map,
151167 on_chunk,
152168 on_source,
153169 on_name,
154170 )
155171 } else {
156172 stream_chunks_of_raw_source (
157- source, options, on_chunk, on_source, on_name,
173+ source. as_ref ( ) ,
174+ options,
175+ on_chunk,
176+ on_source,
177+ on_name,
158178 )
159179 }
160180 }
161181 None => {
162182 let ( generated_info, map) = stream_and_get_source_and_map (
163183 options,
164184 object_pool,
165- & self . inner ,
185+ self . chunks . as_ref ( ) ,
166186 on_chunk,
167187 on_source,
168188 on_name,
@@ -174,6 +194,12 @@ impl StreamChunks for CachedSource {
174194 }
175195}
176196
197+ impl StreamChunks for CachedSource {
198+ fn stream_chunks < ' a > ( & ' a self ) -> Box < dyn Chunks + ' a > {
199+ Box :: new ( CachedSourceChunks :: new ( self ) )
200+ }
201+ }
202+
177203impl Clone for CachedSource {
178204 fn clone ( & self ) -> Self {
179205 Self {
@@ -323,22 +349,26 @@ mod tests {
323349 let mut on_chunk_count = 0 ;
324350 let mut on_source_count = 0 ;
325351 let mut on_name_count = 0 ;
326- let generated_info = source. stream_chunks (
327- & ObjectPool :: default ( ) ,
328- & map_options,
329- & mut |_chunk, _mapping| {
330- on_chunk_count += 1 ;
331- } ,
332- & mut |_source_index, _source, _source_content| {
333- on_source_count += 1 ;
334- } ,
335- & mut |_name_index, _name| {
336- on_name_count += 1 ;
337- } ,
338- ) ;
352+ let generated_info = {
353+ let object_pool = ObjectPool :: default ( ) ;
354+ let chunks = source. stream_chunks ( ) ;
355+ chunks. stream (
356+ & object_pool,
357+ & map_options,
358+ & mut |_chunk, _mapping| {
359+ on_chunk_count += 1 ;
360+ } ,
361+ & mut |_source_index, _source, _source_content| {
362+ on_source_count += 1 ;
363+ } ,
364+ & mut |_name_index, _name| {
365+ on_name_count += 1 ;
366+ } ,
367+ )
368+ } ;
339369
340370 let cached_source = CachedSource :: new ( source) ;
341- cached_source. stream_chunks (
371+ cached_source. stream_chunks ( ) . stream (
342372 & ObjectPool :: default ( ) ,
343373 & map_options,
344374 & mut |_chunk, _mapping| { } ,
@@ -349,7 +379,7 @@ mod tests {
349379 let mut cached_on_chunk_count = 0 ;
350380 let mut cached_on_source_count = 0 ;
351381 let mut cached_on_name_count = 0 ;
352- let cached_generated_info = cached_source. stream_chunks (
382+ let cached_generated_info = cached_source. stream_chunks ( ) . stream (
353383 & ObjectPool :: default ( ) ,
354384 & map_options,
355385 & mut |_chunk, _mapping| {
0 commit comments