@@ -5,8 +5,8 @@ use std::env;
55use std:: fs;
66use std:: io:: Read ;
77use std:: path:: { Path , PathBuf } ;
8- use std:: sync:: Arc ;
98
9+ use bytes_str:: BytesStr ;
1010use debugid:: DebugId ;
1111use rustc_hash:: FxHashMap ;
1212use url:: Url ;
@@ -20,14 +20,14 @@ use crate::types::{RawToken, SourceMap, Token};
2020/// objects is generally not very comfortable. As a general aid this
2121/// type can help.
2222pub struct SourceMapBuilder {
23- file : Option < Arc < str > > ,
24- name_map : FxHashMap < Arc < str > , u32 > ,
25- names : Vec < Arc < str > > ,
23+ file : Option < BytesStr > ,
24+ name_map : FxHashMap < BytesStr , u32 > ,
25+ names : Vec < BytesStr > ,
2626 tokens : Vec < RawToken > ,
27- source_map : FxHashMap < Arc < str > , u32 > ,
28- source_root : Option < Arc < str > > ,
29- sources : Vec < Arc < str > > ,
30- source_contents : Vec < Option < Arc < str > > > ,
27+ source_map : FxHashMap < BytesStr , u32 > ,
28+ source_root : Option < BytesStr > ,
29+ sources : Vec < BytesStr > ,
30+ source_contents : Vec < Option < BytesStr > > ,
3131 sources_mapping : Vec < u32 > ,
3232 ignore_list : BTreeSet < u32 > ,
3333 debug_id : Option < DebugId > ,
@@ -52,9 +52,9 @@ fn resolve_local_reference(base: &Url, reference: &str) -> Option<PathBuf> {
5252
5353impl SourceMapBuilder {
5454 /// Creates a new source map builder and sets the file.
55- pub fn new ( file : Option < & str > ) -> SourceMapBuilder {
55+ pub fn new ( file : Option < BytesStr > ) -> SourceMapBuilder {
5656 SourceMapBuilder {
57- file : file . map ( Into :: into ) ,
57+ file,
5858 name_map : FxHashMap :: default ( ) ,
5959 names : vec ! [ ] ,
6060 tokens : vec ! [ ] ,
@@ -74,7 +74,7 @@ impl SourceMapBuilder {
7474 }
7575
7676 /// Sets the file for the sourcemap (optional)
77- pub fn set_file < T : Into < Arc < str > > > ( & mut self , value : Option < T > ) {
77+ pub fn set_file < T : Into < BytesStr > > ( & mut self , value : Option < T > ) {
7878 self . file = value. map ( Into :: into) ;
7979 }
8080
@@ -84,7 +84,7 @@ impl SourceMapBuilder {
8484 }
8585
8686 /// Sets a new value for the source_root.
87- pub fn set_source_root < T : Into < Arc < str > > > ( & mut self , value : Option < T > ) {
87+ pub fn set_source_root < T : Into < BytesStr > > ( & mut self , value : Option < T > ) {
8888 self . source_root = value. map ( Into :: into) ;
8989 }
9090
@@ -94,24 +94,24 @@ impl SourceMapBuilder {
9494 }
9595
9696 /// Registers a new source with the builder and returns the source ID.
97- pub fn add_source ( & mut self , src : & str ) -> u32 {
97+ pub fn add_source ( & mut self , src : BytesStr ) -> u32 {
9898 self . add_source_with_id ( src, !0 )
9999 }
100100
101- fn add_source_with_id ( & mut self , src : & str , old_id : u32 ) -> u32 {
101+ fn add_source_with_id ( & mut self , src : BytesStr , old_id : u32 ) -> u32 {
102102 let count = self . sources . len ( ) as u32 ;
103- let id = * self . source_map . entry ( src. into ( ) ) . or_insert ( count) ;
103+ let id = * self . source_map . entry ( src. clone ( ) ) . or_insert ( count) ;
104104 if id == count {
105- self . sources . push ( src. into ( ) ) ;
105+ self . sources . push ( src) ;
106106 self . sources_mapping . push ( old_id) ;
107107 }
108108 id
109109 }
110110
111111 /// Changes the source name for an already set source.
112- pub fn set_source ( & mut self , src_id : u32 , src : & str ) {
112+ pub fn set_source ( & mut self , src_id : u32 , src : BytesStr ) {
113113 assert ! ( src_id != !0 , "Cannot set sources for tombstone source id" ) ;
114- self . sources [ src_id as usize ] = src. into ( ) ;
114+ self . sources [ src_id as usize ] = src;
115115 }
116116
117117 /// Looks up a source name for an ID.
@@ -124,12 +124,12 @@ impl SourceMapBuilder {
124124 }
125125
126126 /// Sets the source contents for an already existing source.
127- pub fn set_source_contents ( & mut self , src_id : u32 , contents : Option < & str > ) {
127+ pub fn set_source_contents ( & mut self , src_id : u32 , contents : Option < BytesStr > ) {
128128 assert ! ( src_id != !0 , "Cannot set sources for tombstone source id" ) ;
129129 if self . sources . len ( ) > self . source_contents . len ( ) {
130130 self . source_contents . resize ( self . sources . len ( ) , None ) ;
131131 }
132- self . source_contents [ src_id as usize ] = contents. map ( Into :: into ) ;
132+ self . source_contents [ src_id as usize ] = contents;
133133 }
134134
135135 /// Returns the current source contents for a source.
@@ -169,7 +169,7 @@ impl SourceMapBuilder {
169169 if let Ok ( mut f) = fs:: File :: open ( path) {
170170 let mut contents = String :: new ( ) ;
171171 if f. read_to_string ( & mut contents) . is_ok ( ) {
172- self . set_source_contents ( src_id, Some ( & contents) ) ;
172+ self . set_source_contents ( src_id, Some ( contents. into ( ) ) ) ;
173173 }
174174 }
175175 }
@@ -178,11 +178,11 @@ impl SourceMapBuilder {
178178 }
179179
180180 /// Registers a name with the builder and returns the name ID.
181- pub fn add_name ( & mut self , name : & str ) -> u32 {
181+ pub fn add_name ( & mut self , name : BytesStr ) -> u32 {
182182 let count = self . names . len ( ) as u32 ;
183- let id = * self . name_map . entry ( name. into ( ) ) . or_insert ( count) ;
183+ let id = * self . name_map . entry ( name. clone ( ) ) . or_insert ( count) ;
184184 if id == count {
185- self . names . push ( name. into ( ) ) ;
185+ self . names . push ( name) ;
186186 }
187187 id
188188 }
@@ -195,8 +195,8 @@ impl SourceMapBuilder {
195195 dst_col : u32 ,
196196 src_line : u32 ,
197197 src_col : u32 ,
198- source : Option < & str > ,
199- name : Option < & str > ,
198+ source : Option < BytesStr > ,
199+ name : Option < BytesStr > ,
200200 is_range : bool ,
201201 ) -> RawToken {
202202 self . add_with_id (
@@ -211,9 +211,9 @@ impl SourceMapBuilder {
211211 dst_col : u32 ,
212212 src_line : u32 ,
213213 src_col : u32 ,
214- source : Option < & str > ,
214+ source : Option < BytesStr > ,
215215 source_id : u32 ,
216- name : Option < & str > ,
216+ name : Option < BytesStr > ,
217217 is_range : bool ,
218218 ) -> RawToken {
219219 let src_id = match source {
@@ -273,9 +273,9 @@ impl SourceMapBuilder {
273273 token. get_dst_col ( ) ,
274274 token. get_src_line ( ) ,
275275 token. get_src_col ( ) ,
276- token. get_source ( ) ,
276+ token. get_source ( ) . cloned ( ) ,
277277 token. get_src_id ( ) ,
278- name,
278+ name. cloned ( ) ,
279279 token. is_range ( ) ,
280280 )
281281 }
@@ -289,7 +289,7 @@ impl SourceMapBuilder {
289289 prefix. push ( '/' ) ;
290290 }
291291 if source. starts_with ( & prefix) {
292- * source = source [ prefix. len ( ) .. ] . into ( ) ;
292+ source. advance ( prefix. len ( ) ) ;
293293 break ;
294294 }
295295 }
0 commit comments