@@ -24,7 +24,7 @@ impl State {
24
24
}
25
25
26
26
/// Span with a change [`State`].
27
- #[ derive( Debug , Clone , PartialEq , Eq ) ]
27
+ #[ derive( Clone , PartialEq , Eq ) ]
28
28
struct Span {
29
29
/// Start of this span in parent data
30
30
start : usize ,
@@ -34,6 +34,17 @@ struct Span {
34
34
data : State ,
35
35
}
36
36
37
+ impl std:: fmt:: Debug for Span {
38
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
39
+ let state = match self . data {
40
+ State :: Initial => "initial" ,
41
+ State :: Replaced ( _) => "replaced" ,
42
+ State :: Inserted ( _) => "inserted" ,
43
+ } ;
44
+ write ! ( f, "({}, {}: {state})" , self . start, self . end)
45
+ }
46
+ }
47
+
37
48
/// A container that allows easily replacing chunks of its data
38
49
#[ derive( Debug , Clone , Default ) ]
39
50
pub struct Data {
@@ -97,102 +108,82 @@ impl Data {
97
108
// [^empty]: Leading and trailing ones might be empty if we replace
98
109
// the whole chunk. As an optimization and without loss of generality we
99
110
// don't add empty parts.
100
- let new_parts = {
101
- let Some ( index_of_part_to_split) = self . parts . iter ( ) . position ( |p| {
102
- !p. data . is_inserted ( ) && p. start <= range. start && p. end >= range. end
103
- } ) else {
104
- if tracing:: enabled!( tracing:: Level :: DEBUG ) {
105
- let slices = self
106
- . parts
107
- . iter ( )
108
- . map ( |p| {
109
- (
110
- p. start ,
111
- p. end ,
112
- match p. data {
113
- State :: Initial => "initial" ,
114
- State :: Replaced ( ..) => "replaced" ,
115
- State :: Inserted ( ..) => "inserted" ,
116
- } ,
117
- )
118
- } )
119
- . collect :: < Vec < _ > > ( ) ;
120
- tracing:: debug!(
121
- "no single slice covering {}..{}, current slices: {:?}" ,
122
- range. start,
123
- range. end,
124
- slices,
125
- ) ;
126
- }
111
+ let Some ( index_of_part_to_split) = self
112
+ . parts
113
+ . iter ( )
114
+ . position ( |p| !p. data . is_inserted ( ) && p. start <= range. start && p. end >= range. end )
115
+ else {
116
+ tracing:: debug!(
117
+ "no single slice covering {}..{}, current slices: {:?}" ,
118
+ range. start,
119
+ range. end,
120
+ self . parts,
121
+ ) ;
122
+ return Err ( Error :: MaybeAlreadyReplaced ( range) ) ;
123
+ } ;
127
124
128
- return Err ( Error :: MaybeAlreadyReplaced ( range) ) ;
129
- } ;
125
+ let part_to_split = & self . parts [ index_of_part_to_split] ;
130
126
131
- let part_to_split = & self . parts [ index_of_part_to_split] ;
132
-
133
- // If this replacement matches exactly the part that we would
134
- // otherwise split then we ignore this for now. This means that you
135
- // can replace the exact same range with the exact same content
136
- // multiple times and we'll process and allow it.
137
- //
138
- // This is currently done to alleviate issues like
139
- // rust-lang/rust#51211 although this clause likely wants to be
140
- // removed if that's fixed deeper in the compiler.
141
- if part_to_split. start == range. start && part_to_split. end == range. end {
142
- if let State :: Replaced ( ref replacement) = part_to_split. data {
143
- if & * * replacement == data {
144
- return Ok ( ( ) ) ;
145
- }
127
+ // If this replacement matches exactly the part that we would
128
+ // otherwise split then we ignore this for now. This means that you
129
+ // can replace the exact same range with the exact same content
130
+ // multiple times and we'll process and allow it.
131
+ //
132
+ // This is currently done to alleviate issues like
133
+ // rust-lang/rust#51211 although this clause likely wants to be
134
+ // removed if that's fixed deeper in the compiler.
135
+ if part_to_split. start == range. start && part_to_split. end == range. end {
136
+ if let State :: Replaced ( ref replacement) = part_to_split. data {
137
+ if & * * replacement == data {
138
+ return Ok ( ( ) ) ;
146
139
}
147
140
}
141
+ }
148
142
149
- if part_to_split. data != State :: Initial {
150
- return Err ( Error :: AlreadyReplaced ) ;
151
- }
152
-
153
- let mut new_parts = Vec :: with_capacity ( self . parts . len ( ) + 2 ) ;
143
+ if part_to_split. data != State :: Initial {
144
+ return Err ( Error :: AlreadyReplaced ) ;
145
+ }
154
146
155
- // Previous parts
156
- if let Some ( ps) = self . parts . get ( ..index_of_part_to_split) {
157
- new_parts. extend_from_slice ( ps) ;
158
- }
147
+ let mut new_parts = Vec :: with_capacity ( self . parts . len ( ) + 2 ) ;
159
148
160
- // Keep initial data on left side of part
161
- if range. start > part_to_split. start {
162
- new_parts. push ( Span {
163
- start : part_to_split. start ,
164
- end : range. start ,
165
- data : State :: Initial ,
166
- } ) ;
167
- }
149
+ // Previous parts
150
+ if let Some ( ps) = self . parts . get ( ..index_of_part_to_split) {
151
+ new_parts. extend_from_slice ( ps) ;
152
+ }
168
153
169
- // New part
154
+ // Keep initial data on left side of part
155
+ if range. start > part_to_split. start {
170
156
new_parts. push ( Span {
171
- start : range. start ,
172
- end : range. end ,
173
- data : if insert_only {
174
- State :: Inserted ( data. into ( ) )
175
- } else {
176
- State :: Replaced ( data. into ( ) )
177
- } ,
157
+ start : part_to_split. start ,
158
+ end : range. start ,
159
+ data : State :: Initial ,
178
160
} ) ;
161
+ }
179
162
180
- // Keep initial data on right side of part
181
- if range. end < part_to_split. end {
182
- new_parts. push ( Span {
183
- start : range. end ,
184
- end : part_to_split. end ,
185
- data : State :: Initial ,
186
- } ) ;
187
- }
188
-
189
- // Following parts
190
- if let Some ( ps) = self . parts . get ( index_of_part_to_split + 1 ..) {
191
- new_parts. extend_from_slice ( ps) ;
192
- }
163
+ // New part
164
+ new_parts. push ( Span {
165
+ start : range. start ,
166
+ end : range. end ,
167
+ data : if insert_only {
168
+ State :: Inserted ( data. into ( ) )
169
+ } else {
170
+ State :: Replaced ( data. into ( ) )
171
+ } ,
172
+ } ) ;
173
+
174
+ // Keep initial data on right side of part
175
+ if range. end < part_to_split. end {
176
+ new_parts. push ( Span {
177
+ start : range. end ,
178
+ end : part_to_split. end ,
179
+ data : State :: Initial ,
180
+ } ) ;
181
+ }
193
182
194
- new_parts
195
- } ;
183
+ // Following parts
184
+ if let Some ( ps) = self . parts . get ( index_of_part_to_split + 1 ..) {
185
+ new_parts. extend_from_slice ( ps) ;
186
+ }
196
187
197
188
self . parts = new_parts;
198
189
0 commit comments