@@ -7,6 +7,7 @@ use std::collections::VecDeque;
7
7
use rustc_hash:: FxHashMap ;
8
8
9
9
use crate :: {
10
+ basic_types:: AssertIntoU32 ,
10
11
chunk:: { Chunk , ChunkIdx , ChunkVec } ,
11
12
span:: Span ,
12
13
CowStr , TextSize ,
@@ -31,14 +32,14 @@ pub struct MagicString<'s> {
31
32
chunk_by_end : FxHashMap < TextSize , ChunkIdx > ,
32
33
}
33
34
34
- impl < ' s > MagicString < ' s > {
35
+ impl < ' text > MagicString < ' text > {
35
36
// --- public
36
37
37
- pub fn new ( source : impl Into < CowStr < ' s > > ) -> Self {
38
+ pub fn new ( source : impl Into < CowStr < ' text > > ) -> Self {
38
39
Self :: with_options ( source, Default :: default ( ) )
39
40
}
40
41
41
- pub fn with_options ( source : impl Into < CowStr < ' s > > , options : MagicStringOptions ) -> Self {
42
+ pub fn with_options ( source : impl Into < CowStr < ' text > > , options : MagicStringOptions ) -> Self {
42
43
let source = source. into ( ) ;
43
44
let initial_chunk = Chunk :: new ( Span ( 0 , source. len ( ) ) ) ;
44
45
let mut chunks = ChunkVec :: with_capacity ( 1 ) ;
@@ -64,7 +65,7 @@ impl<'s> MagicString<'s> {
64
65
magic_string
65
66
}
66
67
67
- pub fn append ( & mut self , source : impl Into < CowStr < ' s > > ) -> & mut Self {
68
+ pub fn append ( & mut self , source : impl Into < CowStr < ' text > > ) -> & mut Self {
68
69
self . append_outro ( source. into ( ) ) ;
69
70
self
70
71
}
@@ -79,10 +80,10 @@ impl<'s> MagicString<'s> {
79
80
///```
80
81
pub fn append_left (
81
82
& mut self ,
82
- text_index : TextSize ,
83
- content : impl Into < CowStr < ' s > > ,
83
+ text_index : impl AssertIntoU32 ,
84
+ content : impl Into < CowStr < ' text > > ,
84
85
) -> & mut Self {
85
- match self . by_end_mut ( text_index) {
86
+ match self . by_end_mut ( text_index. assert_into_u32 ( ) ) {
86
87
Some ( chunk) => {
87
88
chunk. append_outro ( content. into ( ) ) ;
88
89
}
@@ -103,10 +104,10 @@ impl<'s> MagicString<'s> {
103
104
///```
104
105
pub fn append_right (
105
106
& mut self ,
106
- text_index : TextSize ,
107
- content : impl Into < CowStr < ' s > > ,
107
+ text_index : impl AssertIntoU32 ,
108
+ content : impl Into < CowStr < ' text > > ,
108
109
) -> & mut Self {
109
- match self . by_start_mut ( text_index) {
110
+ match self . by_start_mut ( text_index. assert_into_u32 ( ) ) {
110
111
Some ( chunk) => {
111
112
chunk. append_intro ( content. into ( ) ) ;
112
113
}
@@ -115,17 +116,17 @@ impl<'s> MagicString<'s> {
115
116
self
116
117
}
117
118
118
- pub fn prepend ( & mut self , source : impl Into < CowStr < ' s > > ) -> & mut Self {
119
+ pub fn prepend ( & mut self , source : impl Into < CowStr < ' text > > ) -> & mut Self {
119
120
self . prepend_intro ( source. into ( ) ) ;
120
121
self
121
122
}
122
123
123
124
pub fn prepend_left (
124
125
& mut self ,
125
- text_index : TextSize ,
126
- content : impl Into < CowStr < ' s > > ,
126
+ text_index : impl AssertIntoU32 ,
127
+ content : impl Into < CowStr < ' text > > ,
127
128
) -> & mut Self {
128
- match self . by_end_mut ( text_index) {
129
+ match self . by_end_mut ( text_index. assert_into_u32 ( ) ) {
129
130
Some ( chunk) => chunk. prepend_outro ( content. into ( ) ) ,
130
131
None => self . prepend_intro ( content. into ( ) ) ,
131
132
}
@@ -134,10 +135,10 @@ impl<'s> MagicString<'s> {
134
135
135
136
pub fn prepend_right (
136
137
& mut self ,
137
- text_index : TextSize ,
138
- content : impl Into < CowStr < ' s > > ,
138
+ text_index : impl AssertIntoU32 ,
139
+ content : impl Into < CowStr < ' text > > ,
139
140
) -> & mut Self {
140
- match self . by_start_mut ( text_index) {
141
+ match self . by_start_mut ( text_index. assert_into_u32 ( ) ) {
141
142
Some ( chunk) => {
142
143
chunk. prepend_intro ( content. into ( ) ) ;
143
144
}
@@ -159,19 +160,19 @@ impl<'s> MagicString<'s> {
159
160
160
161
// --- private
161
162
162
- fn prepend_intro ( & mut self , content : impl Into < CowStr < ' s > > ) {
163
+ fn prepend_intro ( & mut self , content : impl Into < CowStr < ' text > > ) {
163
164
self . intro . push_front ( content. into ( ) ) ;
164
165
}
165
166
166
- fn append_outro ( & mut self , content : impl Into < CowStr < ' s > > ) {
167
+ fn append_outro ( & mut self , content : impl Into < CowStr < ' text > > ) {
167
168
self . outro . push_back ( content. into ( ) ) ;
168
169
}
169
170
170
- fn prepend_outro ( & mut self , content : impl Into < CowStr < ' s > > ) {
171
+ fn prepend_outro ( & mut self , content : impl Into < CowStr < ' text > > ) {
171
172
self . outro . push_front ( content. into ( ) ) ;
172
173
}
173
174
174
- fn append_intro ( & mut self , content : impl Into < CowStr < ' s > > ) {
175
+ fn append_intro ( & mut self , content : impl Into < CowStr < ' text > > ) {
175
176
self . intro . push_back ( content. into ( ) ) ;
176
177
}
177
178
@@ -182,7 +183,7 @@ impl<'s> MagicString<'s> {
182
183
}
183
184
}
184
185
185
- pub ( crate ) fn fragments ( & ' s self ) -> impl Iterator < Item = & ' s str > {
186
+ pub ( crate ) fn fragments ( & ' text self ) -> impl Iterator < Item = & ' text str > {
186
187
let intro = self . intro . iter ( ) . map ( |s| s. as_ref ( ) ) ;
187
188
let outro = self . outro . iter ( ) . map ( |s| s. as_ref ( ) ) ;
188
189
let chunks = self . iter_chunks ( ) . flat_map ( |c| c. fragments ( & self . source ) ) ;
@@ -236,7 +237,8 @@ impl<'s> MagicString<'s> {
236
237
chunk_contains_index. next = Some ( new_chunk_idx) ;
237
238
}
238
239
239
- fn by_start_mut ( & mut self , text_index : TextSize ) -> Option < & mut Chunk < ' s > > {
240
+ fn by_start_mut ( & mut self , text_index : impl AssertIntoU32 ) -> Option < & mut Chunk < ' text > > {
241
+ let text_index = text_index. assert_into_u32 ( ) ;
240
242
if text_index == self . source . len ( ) {
241
243
None
242
244
} else {
@@ -247,7 +249,8 @@ impl<'s> MagicString<'s> {
247
249
}
248
250
}
249
251
250
- fn by_end_mut ( & mut self , text_index : TextSize ) -> Option < & mut Chunk < ' s > > {
252
+ fn by_end_mut ( & mut self , text_index : TextSize ) -> Option < & mut Chunk < ' text > > {
253
+ let text_index = text_index. assert_into_u32 ( ) ;
251
254
if text_index == 0 {
252
255
None
253
256
} else {
@@ -258,11 +261,11 @@ impl<'s> MagicString<'s> {
258
261
}
259
262
}
260
263
261
- fn last_chunk ( & self ) -> & Chunk < ' s > {
264
+ fn last_chunk ( & self ) -> & Chunk < ' text > {
262
265
& self . chunks [ self . last_chunk_idx ]
263
266
}
264
267
265
- fn first_chunk ( & self ) -> & Chunk < ' s > {
268
+ fn first_chunk ( & self ) -> & Chunk < ' text > {
266
269
& self . chunks [ self . first_chunk_idx ]
267
270
}
268
271
}
0 commit comments