@@ -75,6 +75,21 @@ pub struct SpanData {
75
75
pub ctxt : SyntaxContext ,
76
76
}
77
77
78
+ impl SpanData {
79
+ #[ inline]
80
+ pub fn with_lo ( & self , lo : BytePos ) -> Span {
81
+ Span :: new ( lo, self . hi , self . ctxt )
82
+ }
83
+ #[ inline]
84
+ pub fn with_hi ( & self , hi : BytePos ) -> Span {
85
+ Span :: new ( self . lo , hi, self . ctxt )
86
+ }
87
+ #[ inline]
88
+ pub fn with_ctxt ( & self , ctxt : SyntaxContext ) -> Span {
89
+ Span :: new ( self . lo , self . hi , ctxt)
90
+ }
91
+ }
92
+
78
93
// The interner in thread-local, so `Span` shouldn't move between threads.
79
94
impl !Send for Span { }
80
95
impl !Sync for Span { }
@@ -109,38 +124,37 @@ impl Span {
109
124
}
110
125
#[ inline]
111
126
pub fn with_lo ( self , lo : BytePos ) -> Span {
112
- let base = self . data ( ) ;
113
- Span :: new ( lo, base. hi , base. ctxt )
127
+ self . data ( ) . with_lo ( lo)
114
128
}
115
129
#[ inline]
116
130
pub fn hi ( self ) -> BytePos {
117
131
self . data ( ) . hi
118
132
}
119
133
#[ inline]
120
134
pub fn with_hi ( self , hi : BytePos ) -> Span {
121
- let base = self . data ( ) ;
122
- Span :: new ( base. lo , hi, base. ctxt )
135
+ self . data ( ) . with_hi ( hi)
123
136
}
124
137
#[ inline]
125
138
pub fn ctxt ( self ) -> SyntaxContext {
126
139
self . data ( ) . ctxt
127
140
}
128
141
#[ inline]
129
142
pub fn with_ctxt ( self , ctxt : SyntaxContext ) -> Span {
130
- let base = self . data ( ) ;
131
- Span :: new ( base. lo , base. hi , ctxt)
143
+ self . data ( ) . with_ctxt ( ctxt)
132
144
}
133
145
134
146
/// Returns a new span representing just the end-point of this span
135
147
pub fn end_point ( self ) -> Span {
136
- let lo = cmp:: max ( self . hi ( ) . 0 - 1 , self . lo ( ) . 0 ) ;
137
- self . with_lo ( BytePos ( lo) )
148
+ let span = self . data ( ) ;
149
+ let lo = cmp:: max ( span. hi . 0 - 1 , span. lo . 0 ) ;
150
+ span. with_lo ( BytePos ( lo) )
138
151
}
139
152
140
153
/// Returns a new span representing the next character after the end-point of this span
141
154
pub fn next_point ( self ) -> Span {
142
- let lo = cmp:: max ( self . hi ( ) . 0 , self . lo ( ) . 0 + 1 ) ;
143
- Span :: new ( BytePos ( lo) , BytePos ( lo) , self . ctxt ( ) )
155
+ let span = self . data ( ) ;
156
+ let lo = cmp:: max ( span. hi . 0 , span. lo . 0 + 1 ) ;
157
+ Span :: new ( BytePos ( lo) , BytePos ( lo) , span. ctxt )
144
158
}
145
159
146
160
/// Returns `self` if `self` is not the dummy span, and `other` otherwise.
@@ -150,21 +164,27 @@ impl Span {
150
164
151
165
/// Return true if `self` fully encloses `other`.
152
166
pub fn contains ( self , other : Span ) -> bool {
153
- self . lo ( ) <= other. lo ( ) && other. hi ( ) <= self . hi ( )
167
+ let span = self . data ( ) ;
168
+ let other = other. data ( ) ;
169
+ span. lo <= other. lo && other. hi <= span. hi
154
170
}
155
171
156
172
/// Return true if the spans are equal with regards to the source text.
157
173
///
158
174
/// Use this instead of `==` when either span could be generated code,
159
175
/// and you only care that they point to the same bytes of source text.
160
176
pub fn source_equal ( & self , other : & Span ) -> bool {
161
- self . lo ( ) == other. lo ( ) && self . hi ( ) == other. hi ( )
177
+ let span = self . data ( ) ;
178
+ let other = other. data ( ) ;
179
+ span. lo == other. lo && span. hi == other. hi
162
180
}
163
181
164
182
/// Returns `Some(span)`, where the start is trimmed by the end of `other`
165
183
pub fn trim_start ( self , other : Span ) -> Option < Span > {
166
- if self . hi ( ) > other. hi ( ) {
167
- Some ( self . with_lo ( cmp:: max ( self . lo ( ) , other. hi ( ) ) ) )
184
+ let span = self . data ( ) ;
185
+ let other = other. data ( ) ;
186
+ if span. hi > other. hi {
187
+ Some ( span. with_lo ( cmp:: max ( span. lo , other. hi ) ) )
168
188
} else {
169
189
None
170
190
}
@@ -268,29 +288,35 @@ impl Span {
268
288
269
289
/// Return a `Span` that would enclose both `self` and `end`.
270
290
pub fn to ( self , end : Span ) -> Span {
291
+ let span = self . data ( ) ;
292
+ let end = end. data ( ) ;
271
293
Span :: new (
272
- cmp:: min ( self . lo ( ) , end. lo ( ) ) ,
273
- cmp:: max ( self . hi ( ) , end. hi ( ) ) ,
294
+ cmp:: min ( span . lo , end. lo ) ,
295
+ cmp:: max ( span . hi , end. hi ) ,
274
296
// FIXME(jseyfried): self.ctxt should always equal end.ctxt here (c.f. issue #23480)
275
- if self . ctxt ( ) == SyntaxContext :: empty ( ) { end. ctxt ( ) } else { self . ctxt ( ) } ,
297
+ if span . ctxt == SyntaxContext :: empty ( ) { end. ctxt } else { span . ctxt } ,
276
298
)
277
299
}
278
300
279
301
/// Return a `Span` between the end of `self` to the beginning of `end`.
280
302
pub fn between ( self , end : Span ) -> Span {
303
+ let span = self . data ( ) ;
304
+ let end = end. data ( ) ;
281
305
Span :: new (
282
- self . hi ( ) ,
283
- end. lo ( ) ,
284
- if end. ctxt ( ) == SyntaxContext :: empty ( ) { end. ctxt ( ) } else { self . ctxt ( ) } ,
306
+ span . hi ,
307
+ end. lo ,
308
+ if end. ctxt == SyntaxContext :: empty ( ) { end. ctxt } else { span . ctxt } ,
285
309
)
286
310
}
287
311
288
312
/// Return a `Span` between the beginning of `self` to the beginning of `end`.
289
313
pub fn until ( self , end : Span ) -> Span {
314
+ let span = self . data ( ) ;
315
+ let end = end. data ( ) ;
290
316
Span :: new (
291
- self . lo ( ) ,
292
- end. lo ( ) ,
293
- if end. ctxt ( ) == SyntaxContext :: empty ( ) { end. ctxt ( ) } else { self . ctxt ( ) } ,
317
+ span . lo ,
318
+ end. lo ,
319
+ if end. ctxt == SyntaxContext :: empty ( ) { end. ctxt } else { span . ctxt } ,
294
320
)
295
321
}
296
322
}
@@ -316,13 +342,14 @@ impl Default for Span {
316
342
317
343
impl serialize:: UseSpecializedEncodable for Span {
318
344
fn default_encode < S : Encoder > ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
345
+ let span = self . data ( ) ;
319
346
s. emit_struct ( "Span" , 2 , |s| {
320
347
s. emit_struct_field ( "lo" , 0 , |s| {
321
- self . lo ( ) . encode ( s)
348
+ span . lo . encode ( s)
322
349
} ) ?;
323
350
324
351
s. emit_struct_field ( "hi" , 1 , |s| {
325
- self . hi ( ) . encode ( s)
352
+ span . hi . encode ( s)
326
353
} )
327
354
} )
328
355
}
0 commit comments