@@ -1225,37 +1225,47 @@ impl DebuggerVisualizerFile {
1225
1225
#[ derive( Clone ) ]
1226
1226
pub enum SourceFileLines {
1227
1227
/// The source file lines, in decoded (random-access) form.
1228
- Lines { lines : Vec < BytePos > } ,
1228
+ Lines ( Vec < BytePos > ) ,
1229
1229
1230
- /// The source file lines in difference list form. This matches the form
1231
- /// used within metadata, which saves space by exploiting the fact that the
1232
- /// lines list is sorted and individual lines are usually not that long.
1233
- ///
1234
- /// We read it directly from metadata and only decode it into `Lines` form
1235
- /// when necessary. This is a significant performance win, especially for
1236
- /// small crates where very little of `std`'s metadata is used.
1237
- Diffs {
1238
- /// Position of the first line. Note that this is always encoded as a
1239
- /// `BytePos` because it is often much larger than any of the
1240
- /// differences.
1241
- line_start : BytePos ,
1242
-
1243
- /// Always 1, 2, or 4. Always as small as possible, while being big
1244
- /// enough to hold the length of the longest line in the source file.
1245
- /// The 1 case is by far the most common.
1246
- bytes_per_diff : usize ,
1247
-
1248
- /// The number of diffs encoded in `raw_diffs`. Always one less than
1249
- /// the number of lines in the source file.
1250
- num_diffs : usize ,
1251
-
1252
- /// The diffs in "raw" form. Each segment of `bytes_per_diff` length
1253
- /// encodes one little-endian diff. Note that they aren't LEB128
1254
- /// encoded. This makes for much faster decoding. Besides, the
1255
- /// bytes_per_diff==1 case is by far the most common, and LEB128
1256
- /// encoding has no effect on that case.
1257
- raw_diffs : Vec < u8 > ,
1258
- } ,
1230
+ /// The source file lines, in undecoded difference list form.
1231
+ Diffs ( SourceFileDiffs ) ,
1232
+ }
1233
+
1234
+ impl SourceFileLines {
1235
+ pub fn is_lines ( & self ) -> bool {
1236
+ matches ! ( self , SourceFileLines :: Lines ( _) )
1237
+ }
1238
+ }
1239
+
1240
+ /// The source file lines in difference list form. This matches the form
1241
+ /// used within metadata, which saves space by exploiting the fact that the
1242
+ /// lines list is sorted and individual lines are usually not that long.
1243
+ ///
1244
+ /// We read it directly from metadata and only decode it into `Lines` form
1245
+ /// when necessary. This is a significant performance win, especially for
1246
+ /// small crates where very little of `std`'s metadata is used.
1247
+ #[ derive( Clone ) ]
1248
+ pub struct SourceFileDiffs {
1249
+ /// Position of the first line. Note that this is always encoded as a
1250
+ /// `BytePos` because it is often much larger than any of the
1251
+ /// differences.
1252
+ line_start : BytePos ,
1253
+
1254
+ /// Always 1, 2, or 4. Always as small as possible, while being big
1255
+ /// enough to hold the length of the longest line in the source file.
1256
+ /// The 1 case is by far the most common.
1257
+ bytes_per_diff : usize ,
1258
+
1259
+ /// The number of diffs encoded in `raw_diffs`. Always one less than
1260
+ /// the number of lines in the source file.
1261
+ num_diffs : usize ,
1262
+
1263
+ /// The diffs in "raw" form. Each segment of `bytes_per_diff` length
1264
+ /// encodes one little-endian diff. Note that they aren't LEB128
1265
+ /// encoded. This makes for much faster decoding. Besides, the
1266
+ /// bytes_per_diff==1 case is by far the most common, and LEB128
1267
+ /// encoding has no effect on that case.
1268
+ raw_diffs : Vec < u8 > ,
1259
1269
}
1260
1270
1261
1271
/// A single source in the [`SourceMap`].
@@ -1298,6 +1308,8 @@ impl<S: Encoder> Encodable<S> for SourceFile {
1298
1308
s. emit_struct_field ( "start_pos" , false , |s| self . start_pos . encode ( s) ) ?;
1299
1309
s. emit_struct_field ( "end_pos" , false , |s| self . end_pos . encode ( s) ) ?;
1300
1310
s. emit_struct_field ( "lines" , false , |s| {
1311
+ // We are always in `Lines` form by the time we reach here.
1312
+ assert ! ( self . lines. borrow( ) . is_lines( ) ) ;
1301
1313
self . lines ( |lines| {
1302
1314
// Store the length.
1303
1315
s. emit_u32 ( lines. len ( ) as u32 ) ?;
@@ -1384,9 +1396,14 @@ impl<D: Decoder> Decodable<D> for SourceFile {
1384
1396
// Read the difference list.
1385
1397
let num_diffs = num_lines as usize - 1 ;
1386
1398
let raw_diffs = d. read_raw_bytes ( bytes_per_diff * num_diffs) . to_vec ( ) ;
1387
- SourceFileLines :: Diffs { line_start, bytes_per_diff, num_diffs, raw_diffs }
1399
+ SourceFileLines :: Diffs ( SourceFileDiffs {
1400
+ line_start,
1401
+ bytes_per_diff,
1402
+ num_diffs,
1403
+ raw_diffs,
1404
+ } )
1388
1405
} else {
1389
- SourceFileLines :: Lines { lines : vec ! [ ] }
1406
+ SourceFileLines :: Lines ( vec ! [ ] )
1390
1407
}
1391
1408
} ;
1392
1409
let multibyte_chars: Vec < MultiByteChar > = Decodable :: decode ( d) ;
@@ -1448,7 +1465,7 @@ impl SourceFile {
1448
1465
external_src : Lock :: new ( ExternalSource :: Unneeded ) ,
1449
1466
start_pos,
1450
1467
end_pos : Pos :: from_usize ( end_pos) ,
1451
- lines : Lock :: new ( SourceFileLines :: Lines { lines } ) ,
1468
+ lines : Lock :: new ( SourceFileLines :: Lines ( lines) ) ,
1452
1469
multibyte_chars,
1453
1470
non_narrow_chars,
1454
1471
normalized_pos,
@@ -1457,14 +1474,19 @@ impl SourceFile {
1457
1474
}
1458
1475
}
1459
1476
1460
- pub fn lines < F , R > ( & self , mut f : F ) -> R
1477
+ pub fn lines < F , R > ( & self , f : F ) -> R
1461
1478
where
1462
- F : FnMut ( & [ BytePos ] ) -> R ,
1479
+ F : FnOnce ( & [ BytePos ] ) -> R ,
1463
1480
{
1464
1481
let mut guard = self . lines . borrow_mut ( ) ;
1465
1482
match & * guard {
1466
- SourceFileLines :: Lines { lines } => f ( lines) ,
1467
- SourceFileLines :: Diffs { mut line_start, bytes_per_diff, num_diffs, raw_diffs } => {
1483
+ SourceFileLines :: Lines ( lines) => f ( lines) ,
1484
+ SourceFileLines :: Diffs ( SourceFileDiffs {
1485
+ mut line_start,
1486
+ bytes_per_diff,
1487
+ num_diffs,
1488
+ raw_diffs,
1489
+ } ) => {
1468
1490
// Convert from "diffs" form to "lines" form.
1469
1491
let num_lines = num_diffs + 1 ;
1470
1492
let mut lines = Vec :: with_capacity ( num_lines) ;
@@ -1504,7 +1526,7 @@ impl SourceFile {
1504
1526
_ => unreachable ! ( ) ,
1505
1527
}
1506
1528
let res = f ( & lines) ;
1507
- * guard = SourceFileLines :: Lines { lines } ;
1529
+ * guard = SourceFileLines :: Lines ( lines) ;
1508
1530
res
1509
1531
}
1510
1532
}
0 commit comments