Skip to content

Commit 3362a66

Browse files
committed
borrow original/replacement data instead of cloning
1 parent 66728d7 commit 3362a66

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

crates/rustfix/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,34 +216,34 @@ pub fn collect_suggestions<S: ::std::hash::BuildHasher>(
216216
/// 2. Calls [`CodeFix::apply`] to apply suggestions to the source code.
217217
/// 3. Calls [`CodeFix::finish`] to get the "fixed" code.
218218
#[derive(Clone)]
219-
pub struct CodeFix {
220-
data: replace::Data,
219+
pub struct CodeFix<'orig> {
220+
data: replace::Data<'orig>,
221221
/// Whether or not the data has been modified.
222222
modified: bool,
223223
}
224224

225-
impl CodeFix {
225+
impl<'orig> CodeFix<'orig> {
226226
/// Creates a `CodeFix` with the source of a file to modify.
227-
pub fn new(s: &str) -> CodeFix {
227+
pub fn new(s: &'orig str) -> CodeFix<'orig> {
228228
CodeFix {
229229
data: replace::Data::new(s.as_bytes()),
230230
modified: false,
231231
}
232232
}
233233

234234
/// Applies a suggestion to the code.
235-
pub fn apply(&mut self, suggestion: &Suggestion) -> Result<(), Error> {
235+
pub fn apply(&mut self, suggestion: &'orig Suggestion) -> Result<(), Error> {
236236
for solution in &suggestion.solutions {
237237
self.apply_solution(solution)?;
238238
}
239239
Ok(())
240240
}
241241

242242
/// Applies an individual solution from a [`Suggestion`].
243-
pub fn apply_solution(&mut self, solution: &Solution) -> Result<(), Error> {
243+
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.as_bytes())
247247
.inspect_err(|_| self.data.restore())?;
248248
}
249249
self.data.commit();

crates/rustfix/src/replace.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,21 @@
2424
//! If you want to discard uncommitted changes, simply call [`Data::restore`] first.
2525
2626
use std::ops::Range;
27-
use std::rc::Rc;
2827

2928
use crate::error::Error;
3029

3130
/// Data that should replace a particular range of the original.
3231
#[derive(Clone)]
33-
struct Span {
32+
struct Span<'orig> {
3433
/// Span of the parent data to be replaced, inclusive of the start, exclusive of the end.
3534
range: Range<usize>,
3635
/// New data to insert at the `start` position of the `original` data.
37-
data: Rc<[u8]>,
36+
data: &'orig [u8],
3837
/// Whether this data is committed or provisional.
3938
committed: bool,
4039
}
4140

42-
impl std::fmt::Debug for Span {
41+
impl<'orig> std::fmt::Debug for Span<'orig> {
4342
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4443
let state = if self.is_insert() {
4544
"inserted"
@@ -61,11 +60,11 @@ impl std::fmt::Debug for Span {
6160
}
6261
}
6362

64-
impl Span {
65-
fn new(range: Range<usize>, data: &[u8]) -> Self {
63+
impl<'orig> Span<'orig> {
64+
fn new(range: Range<usize>, data: &'orig [u8]) -> Self {
6665
Self {
6766
range,
68-
data: data.into(),
67+
data,
6968
committed: false,
7069
}
7170
}
@@ -79,7 +78,7 @@ impl Span {
7978
}
8079
}
8180

82-
impl PartialEq for Span {
81+
impl<'orig> PartialEq for Span<'orig> {
8382
/// Returns `true` if and only if this `Span` and `other` have the same range and data,
8483
/// regardless of `committed` status.
8584
fn eq(&self, other: &Self) -> bool {
@@ -89,20 +88,20 @@ impl PartialEq for Span {
8988

9089
/// A container that allows easily replacing chunks of its data.
9190
#[derive(Debug, Clone, Default)]
92-
pub struct Data {
91+
pub struct Data<'orig> {
9392
/// Original data.
94-
original: Vec<u8>,
93+
original: &'orig [u8],
9594
/// [`Span`]s covering the full range of the original data.
9695
/// Important: it's expected that the underlying implementation maintains this in order,
9796
/// sorted ascending by start position.
98-
parts: Vec<Span>,
97+
parts: Vec<Span<'orig>>,
9998
}
10099

101-
impl Data {
100+
impl<'orig> Data<'orig> {
102101
/// Create a new data container from a slice of bytes
103-
pub fn new(data: &[u8]) -> Self {
102+
pub fn new(data: &'orig [u8]) -> Self {
104103
Data {
105-
original: data.into(),
104+
original: data.as_ref(),
106105
parts: vec![],
107106
}
108107
}
@@ -126,7 +125,7 @@ impl Data {
126125
if span.range.start > prev_end {
127126
acc.extend_from_slice(&self.original[prev_end..span.range.start]);
128127
}
129-
acc.extend_from_slice(&span.data);
128+
acc.extend_from_slice(span.data);
130129
prev_end = span.range.end;
131130
acc
132131
});
@@ -143,7 +142,7 @@ impl Data {
143142
/// this method will return an error.
144143
/// It will also return an error if the beginning of the range comes before its end,
145144
/// or if the range is outside that of the original data.
146-
pub fn replace_range(&mut self, range: Range<usize>, data: &[u8]) -> Result<(), Error> {
145+
pub fn replace_range(&mut self, range: Range<usize>, data: &'orig [u8]) -> Result<(), Error> {
147146
if range.start > range.end {
148147
return Err(Error::InvalidRange(range));
149148
}

0 commit comments

Comments
 (0)