@@ -1225,37 +1225,47 @@ impl DebuggerVisualizerFile {
12251225#[ derive( Clone ) ]
12261226pub enum SourceFileLines {
12271227 /// The source file lines, in decoded (random-access) form.
1228- Lines { lines : Vec < BytePos > } ,
1228+ Lines ( Vec < BytePos > ) ,
12291229
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 > ,
12591269}
12601270
12611271/// A single source in the [`SourceMap`].
@@ -1298,6 +1308,8 @@ impl<S: Encoder> Encodable<S> for SourceFile {
12981308 s. emit_struct_field ( "start_pos" , false , |s| self . start_pos . encode ( s) ) ?;
12991309 s. emit_struct_field ( "end_pos" , false , |s| self . end_pos . encode ( s) ) ?;
13001310 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( ) ) ;
13011313 self . lines ( |lines| {
13021314 // Store the length.
13031315 s. emit_u32 ( lines. len ( ) as u32 ) ?;
@@ -1384,9 +1396,14 @@ impl<D: Decoder> Decodable<D> for SourceFile {
13841396 // Read the difference list.
13851397 let num_diffs = num_lines as usize - 1 ;
13861398 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+ } )
13881405 } else {
1389- SourceFileLines :: Lines { lines : vec ! [ ] }
1406+ SourceFileLines :: Lines ( vec ! [ ] )
13901407 }
13911408 } ;
13921409 let multibyte_chars: Vec < MultiByteChar > = Decodable :: decode ( d) ;
@@ -1448,7 +1465,7 @@ impl SourceFile {
14481465 external_src : Lock :: new ( ExternalSource :: Unneeded ) ,
14491466 start_pos,
14501467 end_pos : Pos :: from_usize ( end_pos) ,
1451- lines : Lock :: new ( SourceFileLines :: Lines { lines } ) ,
1468+ lines : Lock :: new ( SourceFileLines :: Lines ( lines) ) ,
14521469 multibyte_chars,
14531470 non_narrow_chars,
14541471 normalized_pos,
@@ -1457,14 +1474,19 @@ impl SourceFile {
14571474 }
14581475 }
14591476
1460- pub fn lines < F , R > ( & self , mut f : F ) -> R
1477+ pub fn lines < F , R > ( & self , f : F ) -> R
14611478 where
1462- F : FnMut ( & [ BytePos ] ) -> R ,
1479+ F : FnOnce ( & [ BytePos ] ) -> R ,
14631480 {
14641481 let mut guard = self . lines . borrow_mut ( ) ;
14651482 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+ } ) => {
14681490 // Convert from "diffs" form to "lines" form.
14691491 let num_lines = num_diffs + 1 ;
14701492 let mut lines = Vec :: with_capacity ( num_lines) ;
@@ -1504,7 +1526,7 @@ impl SourceFile {
15041526 _ => unreachable ! ( ) ,
15051527 }
15061528 let res = f ( & lines) ;
1507- * guard = SourceFileLines :: Lines { lines } ;
1529+ * guard = SourceFileLines :: Lines ( lines) ;
15081530 res
15091531 }
15101532 }
0 commit comments