@@ -12,8 +12,8 @@ use crate::{
1212 linear_map:: LinearMap ,
1313 object_pool:: ObjectPool ,
1414 source_content_lines:: SourceContentLines ,
15- BoxSource , MapOptions , Mapping , OriginalLocation , OriginalSource , Source ,
16- SourceExt , SourceMap , SourceValue ,
15+ BoxSource , MapOptions , Mapping , OriginalLocation , OriginalSource , Rope ,
16+ Source , SourceExt , SourceMap , SourceValue ,
1717} ;
1818
1919/// Decorates a Source with replacements and insertions of source code,
@@ -161,24 +161,30 @@ impl ReplaceSource {
161161
162162impl Source for ReplaceSource {
163163 fn source ( & self ) -> SourceValue {
164- if self . replacements . is_empty ( ) {
165- return self . inner . source ( ) ;
166- }
167- let mut string = String :: with_capacity ( self . size ( ) ) ;
168- for chunk in self . rope ( ) {
169- string. push_str ( chunk) ;
164+ match self . rope ( ) {
165+ Rope :: Light ( s) => SourceValue :: String ( Cow :: Borrowed ( s) ) ,
166+ Rope :: Full ( iter) => {
167+ let mut string = String :: with_capacity ( self . size ( ) ) ;
168+ for chunk in iter {
169+ string. push_str ( chunk) ;
170+ }
171+ SourceValue :: String ( Cow :: Owned ( string) )
172+ }
170173 }
171- SourceValue :: String ( Cow :: Owned ( string) )
172174 }
173175
174- fn rope ( & self ) -> Box < dyn Iterator < Item = & str > + ' _ > {
176+ fn rope ( & self ) -> Rope {
175177 if self . replacements . is_empty ( ) {
176178 return self . inner . rope ( ) ;
177179 }
178- Box :: new ( ReplaceSourceRopeIterator :: new (
179- self . inner . rope ( ) ,
180+ let inner_chunks = match self . inner . rope ( ) {
181+ Rope :: Light ( s) => Box :: new ( std:: iter:: once ( s) ) ,
182+ Rope :: Full ( iter) => iter,
183+ } ;
184+ Rope :: Full ( Box :: new ( ReplaceSourceRopeIterator :: new (
185+ inner_chunks,
180186 & self . replacements ,
181- ) )
187+ ) ) )
182188 }
183189
184190 fn buffer ( & self ) -> Cow < [ u8 ] > {
@@ -239,8 +245,13 @@ impl Source for ReplaceSource {
239245 }
240246
241247 fn to_writer ( & self , writer : & mut dyn std:: io:: Write ) -> std:: io:: Result < ( ) > {
242- for text in self . rope ( ) {
243- writer. write_all ( text. as_bytes ( ) ) ?;
248+ match self . rope ( ) {
249+ Rope :: Light ( s) => writer. write_all ( s. as_bytes ( ) ) ?,
250+ Rope :: Full ( iter) => {
251+ for chunk in iter {
252+ writer. write_all ( chunk. as_bytes ( ) ) ?
253+ }
254+ }
244255 }
245256 Ok ( ( ) )
246257 }
0 commit comments