@@ -142,6 +142,23 @@ impl Data {
142
142
} ) ?;
143
143
144
144
let part_to_split = & self . parts [ index_of_part_to_split] ;
145
+
146
+ // If this replacement matches exactly the part that we would
147
+ // otherwise split then we ignore this for now. This means that you
148
+ // can replace the exact same range with the exact same content
149
+ // multiple times and we'll process and allow it.
150
+ //
151
+ // This is currently done to alleviate issues like
152
+ // rust-lang/rust#51211 although this clause likely wants to be
153
+ // removed if that's fixed deeper in the compiler.
154
+ if part_to_split. start == from && part_to_split. end == up_to_and_including {
155
+ if let State :: Replaced ( ref replacement) = part_to_split. data {
156
+ if & * * replacement == data {
157
+ return Ok ( ( ) )
158
+ }
159
+ }
160
+ }
161
+
145
162
ensure ! (
146
163
part_to_split. data == State :: Initial ,
147
164
"Cannot replace slice of data that was already replaced"
@@ -267,7 +284,7 @@ mod tests {
267
284
d. replace_range ( 4 , 6 , b"lol" ) . unwrap ( ) ;
268
285
assert_eq ! ( "foo lol baz" , str ( & d. to_vec( ) ) ) ;
269
286
270
- d. replace_range ( 4 , 6 , b"lol " ) . unwrap ( ) ;
287
+ d. replace_range ( 4 , 6 , b"lol2 " ) . unwrap ( ) ;
271
288
}
272
289
273
290
#[ test]
@@ -277,6 +294,14 @@ mod tests {
277
294
d. replace_range ( 4 , 7 , b"lol" ) . unwrap ( ) ;
278
295
}
279
296
297
+ #[ test]
298
+ fn replace_same_twice ( ) {
299
+ let mut d = Data :: new ( b"foo" ) ;
300
+ d. replace_range ( 0 , 0 , b"b" ) . unwrap ( ) ;
301
+ d. replace_range ( 0 , 0 , b"b" ) . unwrap ( ) ;
302
+ assert_eq ! ( "boo" , str ( & d. to_vec( ) ) ) ;
303
+ }
304
+
280
305
proptest ! {
281
306
#[ test]
282
307
#[ ignore]
0 commit comments