@@ -20,7 +20,8 @@ use std::sync::Arc;
2020
2121struct FileId {
2222 name : PathBuf ,
23- hash : u64 , // Hash of name
23+ /// Hash value of `self.name`.
24+ hash : u64 ,
2425}
2526
2627impl FileId {
@@ -50,13 +51,14 @@ fn hash(value: &Path) -> u64 {
5051 hasher. finish ( )
5152}
5253
54+ /// Represents a single source file and its contents.
5355struct UniqueSource {
5456 file_id : FileId ,
5557 contents : RwLock < Contents > ,
5658}
5759
5860impl fmt:: Debug for UniqueSource {
59- /// Custom implementation to avoid large contents strings
61+ /// Custom implementation to avoid large contents strings.
6062 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
6163 write ! ( f, "Source {{file_name: {:?}}}" , self . file_name( ) )
6264 }
@@ -96,6 +98,8 @@ impl UniqueSource {
9698 }
9799}
98100
101+ /// A thread-safe reference to a source file.
102+ /// Multiple objects of this type can refer to the same source.
99103#[ derive( Debug , Clone ) ]
100104pub struct Source {
101105 source : Arc < UniqueSource > ,
@@ -116,6 +120,10 @@ impl Hash for Source {
116120}
117121
118122impl Source {
123+ /// Creates a source from a (virtual) name and in-memory contents.
124+ ///
125+ /// Note: For differing values of `contents`, the value of `file_name`
126+ /// *must* differ as well.
119127 pub fn inline ( file_name : & Path , contents : & str ) -> Source {
120128 Source {
121129 source : Arc :: new ( UniqueSource :: inline ( file_name, contents) ) ,
@@ -160,9 +168,12 @@ impl Source {
160168 }
161169}
162170
171+ /// A lexical position (line, column) in a source.
163172#[ derive( PartialEq , Eq , PartialOrd , Ord , Clone , Copy , Hash , Debug ) ]
164173pub struct Position {
174+ /// Line (zero-based).
165175 pub line : u32 ,
176+ /// Column (zero-based).
166177 pub character : u32 ,
167178}
168179
@@ -211,9 +222,12 @@ impl Position {
211222 }
212223}
213224
225+ /// A lexical range in a source.
214226#[ derive( PartialEq , Eq , Clone , Copy , Hash , Debug ) ]
215227pub struct Range {
228+ /// Start of the range (inclusive).
216229 pub start : Position ,
230+ /// End of the range (exclusive).
217231 pub end : Position ,
218232}
219233
@@ -223,14 +237,15 @@ impl Range {
223237 }
224238}
225239
226- /// Lexical position in a file.
240+ /// A lexical range within a specific source file.
227241#[ derive( PartialEq , Clone , Debug , Eq , Hash ) ]
228242pub struct SrcPos {
229- /// The source
243+ /// The referenced source file.
230244 pub source : Source ,
231245 range : Range ,
232246}
233247
248+ /// A generic object with an associated source file and lexical range.
234249#[ derive( PartialEq , Clone , Debug ) ]
235250pub struct WithPos < T > {
236251 pub item : T ,
@@ -261,6 +276,7 @@ impl<T> WithPos<T> {
261276 pos : self . pos ,
262277 }
263278 }
279+
264280 pub fn try_map_into < F , U > ( self , f : F ) -> DiagnosticResult < WithPos < U > >
265281 where
266282 F : FnOnce ( T ) -> Result < U , String > ,
@@ -422,7 +438,7 @@ impl SrcPos {
422438 ( lineno_len, result)
423439 }
424440
425- /// Create a string for pretty printing
441+ /// Create a string for pretty printing.
426442 pub fn code_context ( & self ) -> String {
427443 self . lineno_len_and_code_context ( ) . 1
428444 }
@@ -457,8 +473,8 @@ impl SrcPos {
457473 result
458474 }
459475
460- /// Combines two lexical positions into a larger legical position overlapping both
461- /// The file name is assumed to be the same
476+ /// Combines two lexical positions into a larger lexical position overlapping both.
477+ /// The file name is assumed to be the same.
462478 pub fn combine_into ( self , other : & dyn AsRef < Self > ) -> Self {
463479 let other = other. as_ref ( ) ;
464480 debug_assert ! ( self . source == other. source, "Assumes sources are equal" ) ;
@@ -493,6 +509,10 @@ impl SrcPos {
493509 }
494510}
495511
512+ /// Denotes an item with an associated source file.
513+ ///
514+ /// Most types that implement this trait do so through the blanket implementation
515+ /// on [`HasSrcPos`](trait.HasSrcPos.html).
496516pub trait HasSource {
497517 fn source ( & self ) -> & Source ;
498518}
@@ -503,6 +523,7 @@ impl HasSource for Source {
503523 }
504524}
505525
526+ /// Denotes an item with an associated lexical range in a source file.
506527pub trait HasSrcPos {
507528 fn pos ( & self ) -> & SrcPos ;
508529}
0 commit comments