1
1
use std:: {
2
- borrow:: Cow ,
3
- cell:: RefCell ,
2
+ borrow:: { Borrow , Cow } ,
3
+ cell:: { RefCell , UnsafeCell } ,
4
4
hash:: { Hash , Hasher } ,
5
5
sync:: {
6
6
atomic:: { AtomicBool , Ordering } ,
@@ -265,7 +265,7 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
265
265
on_name : crate :: helpers:: OnName < ' _ , ' a > ,
266
266
) -> crate :: helpers:: GeneratedInfo {
267
267
self . sort_replacement ( ) ;
268
- let on_name = RefCell :: new ( on_name) ;
268
+ let on_name = UnsafeCell :: new ( on_name) ;
269
269
let repls = self . replacements ( ) ;
270
270
let mut pos: u32 = 0 ;
271
271
let mut i: usize = 0 ;
@@ -274,12 +274,12 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
274
274
let mut generated_line_offset: i64 = 0 ;
275
275
let mut generated_column_offset: i64 = 0 ;
276
276
let mut generated_column_offset_line = 0 ;
277
- let source_content_lines: RefCell < Vec < Option < Vec < & str > > > > =
278
- RefCell :: new ( Vec :: new ( ) ) ;
279
- let name_mapping: RefCell < HashMap < Cow < str > , u32 > > =
280
- RefCell :: new ( HashMap :: default ( ) ) ;
281
- let name_index_mapping: RefCell < HashMap < u32 , u32 > > =
282
- RefCell :: new ( HashMap :: default ( ) ) ;
277
+ let source_content_lines: UnsafeCell < Vec < Option < Vec < & str > > > > =
278
+ UnsafeCell :: new ( Vec :: new ( ) ) ;
279
+ let name_mapping: UnsafeCell < HashMap < Cow < str > , u32 > > =
280
+ UnsafeCell :: new ( HashMap :: default ( ) ) ;
281
+ let name_index_mapping: UnsafeCell < HashMap < u32 , u32 > > =
282
+ UnsafeCell :: new ( HashMap :: default ( ) ) ;
283
283
284
284
// check if source_content[line][col] is equal to expect
285
285
// Why this is needed?
@@ -309,8 +309,9 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
309
309
// webpack-sources also have this function, refer https://github.com/webpack/webpack-sources/blob/main/lib/ReplaceSource.js#L158
310
310
let check_original_content =
311
311
|source_index : u32 , line : u32 , column : u32 , expected_chunk : & str | {
312
+ let source_content_lines = unsafe { & mut * source_content_lines. get ( ) } ;
312
313
if let Some ( Some ( content_lines) ) =
313
- source_content_lines. borrow ( ) . get ( source_index as usize )
314
+ source_content_lines. get ( source_index as usize )
314
315
{
315
316
if let Some ( content_line) = content_lines. get ( line as usize - 1 ) {
316
317
WithIndices :: new ( content_line) . substring (
@@ -405,7 +406,8 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
405
406
original_line : original. original_line ,
406
407
original_column : original. original_column ,
407
408
name_index : original. name_index . and_then ( |name_index| {
408
- name_index_mapping. borrow ( ) . get ( & name_index) . copied ( )
409
+ let name_index_mapping = unsafe { & mut * name_index_mapping. get ( ) } ;
410
+ name_index_mapping. get ( & name_index) . copied ( )
409
411
} ) ,
410
412
}
411
413
} ) ,
@@ -437,12 +439,13 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
437
439
if let Some ( name) =
438
440
repl. name . as_ref ( ) . filter ( |_| mapping. original . is_some ( ) )
439
441
{
440
- let mut name_mapping = name_mapping. borrow_mut ( ) ;
442
+ let name_mapping = unsafe { & mut * name_mapping. get ( ) } ;
441
443
let mut global_index = name_mapping. get ( name. as_str ( ) ) . copied ( ) ;
442
444
if global_index. is_none ( ) {
443
445
let len = name_mapping. len ( ) as u32 ;
444
446
name_mapping. insert ( Cow :: Borrowed ( name) , len) ;
445
- on_name. borrow_mut ( ) ( len, Cow :: Owned ( name. to_string ( ) ) ) ;
447
+ let on_name = unsafe { & mut * on_name. get ( ) } ;
448
+ on_name ( len, Cow :: Owned ( name. to_string ( ) ) ) ;
446
449
global_index = Some ( len) ;
447
450
}
448
451
replacement_name_index = global_index;
@@ -582,7 +585,8 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
582
585
original_line : original. original_line ,
583
586
original_column : original. original_column ,
584
587
name_index : original. name_index . and_then ( |name_index| {
585
- name_index_mapping. borrow ( ) . get ( & name_index) . copied ( )
588
+ let name_index_mapping = unsafe { & mut * name_index_mapping. get ( ) } ;
589
+ name_index_mapping. get ( & name_index) . copied ( )
586
590
} ) ,
587
591
}
588
592
} ) ,
@@ -592,7 +596,7 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
592
596
pos = end_pos;
593
597
} ,
594
598
& mut |source_index, source, source_content| {
595
- let mut source_content_lines = source_content_lines. borrow_mut ( ) ;
599
+ let source_content_lines = unsafe { & mut * source_content_lines. get ( ) } ;
596
600
while source_content_lines. len ( ) <= source_index as usize {
597
601
source_content_lines. push ( None ) ;
598
602
}
@@ -601,16 +605,17 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
601
605
on_source ( source_index, source, source_content) ;
602
606
} ,
603
607
& mut |name_index, name| {
604
- let mut name_mapping = name_mapping. borrow_mut ( ) ;
608
+ let name_mapping = unsafe { & mut * name_mapping. get ( ) } ;
605
609
let mut global_index = name_mapping. get ( & name) . copied ( ) ;
606
610
if global_index. is_none ( ) {
607
611
let len = name_mapping. len ( ) as u32 ;
608
612
name_mapping. insert ( name. clone ( ) , len) ;
609
- on_name. borrow_mut ( ) ( len, name) ;
613
+ let on_name = unsafe { & mut * on_name. get ( ) } ;
614
+ on_name ( len, name) ;
610
615
global_index = Some ( len) ;
611
616
}
617
+ let name_index_mapping = unsafe { & mut * name_index_mapping. get ( ) } ;
612
618
name_index_mapping
613
- . borrow_mut ( )
614
619
. insert ( name_index, global_index. unwrap ( ) ) ;
615
620
} ,
616
621
) ;
0 commit comments