Skip to content

Commit 84d83ca

Browse files
committed
accept both &[u8] and &str in rustfix methods
1 parent 3362a66 commit 84d83ca

File tree

2 files changed

+55
-42
lines changed

2 files changed

+55
-42
lines changed

crates/rustfix/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl<'orig> CodeFix<'orig> {
226226
/// Creates a `CodeFix` with the source of a file to modify.
227227
pub fn new(s: &'orig str) -> CodeFix<'orig> {
228228
CodeFix {
229-
data: replace::Data::new(s.as_bytes()),
229+
data: replace::Data::new(s),
230230
modified: false,
231231
}
232232
}
@@ -243,7 +243,7 @@ impl<'orig> CodeFix<'orig> {
243243
pub fn apply_solution(&mut self, solution: &'orig Solution) -> Result<(), Error> {
244244
for r in &solution.replacements {
245245
self.data
246-
.replace_range(r.snippet.range.clone(), &r.replacement.as_bytes())
246+
.replace_range(r.snippet.range.clone(), &r.replacement)
247247
.inspect_err(|_| self.data.restore())?;
248248
}
249249
self.data.commit();

crates/rustfix/src/replace.rs

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ pub struct Data<'orig> {
9999

100100
impl<'orig> Data<'orig> {
101101
/// Create a new data container from a slice of bytes
102-
pub fn new(data: &'orig [u8]) -> Self {
102+
pub fn new<T>(data: &'orig T) -> Self
103+
where
104+
T: AsRef<[u8]> + ?Sized,
105+
{
103106
Data {
104107
original: data.as_ref(),
105108
parts: vec![],
@@ -142,7 +145,10 @@ impl<'orig> Data<'orig> {
142145
/// this method will return an error.
143146
/// It will also return an error if the beginning of the range comes before its end,
144147
/// or if the range is outside that of the original data.
145-
pub fn replace_range(&mut self, range: Range<usize>, data: &'orig [u8]) -> Result<(), Error> {
148+
pub fn replace_range<T>(&mut self, range: Range<usize>, data: &'orig T) -> Result<(), Error>
149+
where
150+
T: AsRef<[u8]> + ?Sized,
151+
{
146152
if range.start > range.end {
147153
return Err(Error::InvalidRange(range));
148154
}
@@ -198,63 +204,63 @@ mod tests {
198204

199205
#[test]
200206
fn insert_at_beginning() {
201-
let mut d = Data::new(b"foo bar baz");
202-
d.replace_range(0..0, b"oh no ").unwrap();
207+
let mut d = Data::new("foo bar baz");
208+
d.replace_range(0..0, "oh no ").unwrap();
203209
assert_eq!("oh no foo bar baz", str(&d.to_vec()));
204210
}
205211

206212
#[test]
207213
fn insert_at_end() {
208-
let mut d = Data::new(b"foo bar baz");
209-
d.replace_range(11..11, b" oh no").unwrap();
214+
let mut d = Data::new("foo bar baz");
215+
d.replace_range(11..11, " oh no").unwrap();
210216
assert_eq!("foo bar baz oh no", str(&d.to_vec()));
211217
}
212218

213219
#[test]
214220
fn replace_some_stuff() {
215-
let mut d = Data::new(b"foo bar baz");
216-
d.replace_range(4..7, b"lol").unwrap();
221+
let mut d = Data::new("foo bar baz");
222+
d.replace_range(4..7, "lol").unwrap();
217223
assert_eq!("foo lol baz", str(&d.to_vec()));
218224
}
219225

220226
#[test]
221227
fn replace_a_single_char() {
222-
let mut d = Data::new(b"let y = true;");
223-
d.replace_range(4..5, b"mut y").unwrap();
228+
let mut d = Data::new("let y = true;");
229+
d.replace_range(4..5, "mut y").unwrap();
224230
assert_eq!("let mut y = true;", str(&d.to_vec()));
225231
}
226232

227233
#[test]
228234
fn replace_multiple_lines() {
229-
let mut d = Data::new(b"lorem\nipsum\ndolor");
235+
let mut d = Data::new("lorem\nipsum\ndolor");
230236

231-
d.replace_range(6..11, b"lol").unwrap();
237+
d.replace_range(6..11, "lol").unwrap();
232238
assert_eq!("lorem\nlol\ndolor", str(&d.to_vec()));
233239

234-
d.replace_range(12..17, b"lol").unwrap();
240+
d.replace_range(12..17, "lol").unwrap();
235241
assert_eq!("lorem\nlol\nlol", str(&d.to_vec()));
236242
}
237243

238244
#[test]
239245
fn replace_multiple_lines_with_insert_only() {
240-
let mut d = Data::new(b"foo!");
246+
let mut d = Data::new("foo!");
241247

242-
d.replace_range(3..3, b"bar").unwrap();
248+
d.replace_range(3..3, "bar").unwrap();
243249
assert_eq!("foobar!", str(&d.to_vec()));
244250

245-
d.replace_range(0..3, b"baz").unwrap();
251+
d.replace_range(0..3, "baz").unwrap();
246252
assert_eq!("bazbar!", str(&d.to_vec()));
247253

248-
d.replace_range(3..4, b"?").unwrap();
254+
d.replace_range(3..4, "?").unwrap();
249255
assert_eq!("bazbar?", str(&d.to_vec()));
250256
}
251257

252258
#[test]
253259
fn replace_invalid_range() {
254-
let mut d = Data::new(b"foo!");
260+
let mut d = Data::new("foo!");
255261

256-
assert!(d.replace_range(2..1, b"bar").is_err());
257-
assert!(d.replace_range(0..3, b"bar").is_ok());
262+
assert!(d.replace_range(2..1, "bar").is_err());
263+
assert!(d.replace_range(0..3, "bar").is_ok());
258264
}
259265

260266
#[test]
@@ -265,13 +271,13 @@ mod tests {
265271

266272
#[test]
267273
fn replace_same_range_diff_data() {
268-
let mut d = Data::new(b"foo bar baz");
274+
let mut d = Data::new("foo bar baz");
269275

270-
d.replace_range(4..7, b"lol").unwrap();
276+
d.replace_range(4..7, "lol").unwrap();
271277
assert_eq!("foo lol baz", str(&d.to_vec()));
272278

273279
assert!(matches!(
274-
d.replace_range(4..7, b"lol2").unwrap_err(),
280+
d.replace_range(4..7, "lol2").unwrap_err(),
275281
Error::AlreadyReplaced {
276282
is_identical: false,
277283
..
@@ -281,13 +287,13 @@ mod tests {
281287

282288
#[test]
283289
fn replace_same_range_same_data() {
284-
let mut d = Data::new(b"foo bar baz");
290+
let mut d = Data::new("foo bar baz");
285291

286-
d.replace_range(4..7, b"lol").unwrap();
292+
d.replace_range(4..7, "lol").unwrap();
287293
assert_eq!("foo lol baz", str(&d.to_vec()));
288294

289295
assert!(matches!(
290-
d.replace_range(4..7, b"lol").unwrap_err(),
296+
d.replace_range(4..7, "lol").unwrap_err(),
291297
Error::AlreadyReplaced {
292298
is_identical: true,
293299
..
@@ -297,28 +303,35 @@ mod tests {
297303

298304
#[test]
299305
fn broken_replacements() {
300-
let mut d = Data::new(b"foo");
306+
let mut d = Data::new("foo");
301307
assert!(matches!(
302-
d.replace_range(4..8, b"lol").unwrap_err(),
308+
d.replace_range(4..8, "lol").unwrap_err(),
303309
Error::DataLengthExceeded(std::ops::Range { start: 4, end: 8 }, 3),
304310
));
305311
}
306312

307313
#[test]
308-
fn replace_same_twice() {
309-
let mut d = Data::new(b"foo");
310-
d.replace_range(0..1, b"b").unwrap();
311-
d.replace_range(0..1, b"b").unwrap_err();
312-
assert_eq!("boo", str(&d.to_vec()));
314+
fn insert_same_twice() {
315+
let mut d = Data::new("foo");
316+
d.replace_range(1..1, "b").unwrap();
317+
assert_eq!("fboo", str(&d.to_vec()));
318+
assert!(matches!(
319+
d.replace_range(1..1, "b").unwrap_err(),
320+
Error::AlreadyReplaced {
321+
is_identical: true,
322+
..
323+
},
324+
));
325+
assert_eq!("fboo", str(&d.to_vec()));
313326
}
314327

315328
#[test]
316329
fn commit_restore() {
317-
let mut d = Data::new(b", ");
330+
let mut d = Data::new(", ");
318331
assert_eq!(", ", str(&d.to_vec()));
319332

320-
d.replace_range(2..2, b"world").unwrap();
321-
d.replace_range(0..0, b"hello").unwrap();
333+
d.replace_range(2..2, "world").unwrap();
334+
d.replace_range(0..0, "hello").unwrap();
322335
assert_eq!("hello, world", str(&d.to_vec()));
323336

324337
d.restore();
@@ -327,14 +340,14 @@ mod tests {
327340
d.commit();
328341
assert_eq!(", ", str(&d.to_vec()));
329342

330-
d.replace_range(2..2, b"world").unwrap();
343+
d.replace_range(2..2, "world").unwrap();
331344
assert_eq!(", world", str(&d.to_vec()));
332345
d.commit();
333346
assert_eq!(", world", str(&d.to_vec()));
334347
d.restore();
335348
assert_eq!(", world", str(&d.to_vec()));
336349

337-
d.replace_range(0..0, b"hello").unwrap();
350+
d.replace_range(0..0, "hello").unwrap();
338351
assert_eq!("hello, world", str(&d.to_vec()));
339352
d.commit();
340353
assert_eq!("hello, world", str(&d.to_vec()));
@@ -345,7 +358,7 @@ mod tests {
345358
proptest! {
346359
#[test]
347360
fn new_to_vec_roundtrip(ref s in "\\PC*") {
348-
assert_eq!(s.as_bytes(), Data::new(s.as_bytes()).to_vec().as_slice());
361+
assert_eq!(s.as_bytes(), &Data::new(s).to_vec());
349362
}
350363

351364
#[test]
@@ -356,7 +369,7 @@ mod tests {
356369
1..100,
357370
)
358371
) {
359-
let mut d = Data::new(data.as_bytes());
372+
let mut d = Data::new(data);
360373
for &(ref range, ref bytes) in replacements {
361374
let _ = d.replace_range(range.clone(), bytes);
362375
}

0 commit comments

Comments
 (0)