@@ -21,7 +21,10 @@ pub trait Input {
2121 fn start ( & self ) -> Self :: Offset ;
2222
2323 /// Get the next offset from the provided one, and the next token if it exists
24- fn next ( & self , offset : Self :: Offset ) -> ( Self :: Offset , Option < Self :: Token > ) ;
24+ ///
25+ /// Safety: `offset` must be generated be generated by either `Input::start` or a previous call to this function,
26+ /// on this input only.
27+ unsafe fn next ( & self , offset : Self :: Offset ) -> ( Self :: Offset , Option < Self :: Token > ) ;
2528
2629 /// Create a span from a start and end offset
2730 fn span ( & self , range : Range < Self :: Offset > ) -> Self :: Span ;
@@ -55,7 +58,8 @@ impl Input for str {
5558 0
5659 }
5760
58- fn next ( & self , offset : Self :: Offset ) -> ( Self :: Offset , Option < Self :: Token > ) {
61+ #[ inline]
62+ unsafe fn next ( & self , offset : Self :: Offset ) -> ( Self :: Offset , Option < Self :: Token > ) {
5963 if offset < self . len ( ) {
6064 let c = unsafe {
6165 self . get_unchecked ( offset..)
@@ -69,6 +73,7 @@ impl Input for str {
6973 }
7074 }
7175
76+ #[ inline]
7277 fn span ( & self , range : Range < Self :: Offset > ) -> Self :: Span {
7378 range. into ( )
7479 }
@@ -79,9 +84,12 @@ impl StrInput<char> for str {}
7984impl SliceInput for str {
8085 type Slice = str ;
8186
87+ #[ inline]
8288 fn slice ( & self , range : Range < Self :: Offset > ) -> & Self :: Slice {
8389 & self [ range]
8490 }
91+
92+ #[ inline]
8593 fn slice_from ( & self , from : RangeFrom < Self :: Offset > ) -> & Self :: Slice {
8694 & self [ from]
8795 }
@@ -96,14 +104,17 @@ impl<T: Clone> Input for [T] {
96104 0
97105 }
98106
99- fn next ( & self , offset : Self :: Offset ) -> ( Self :: Offset , Option < Self :: Token > ) {
107+ #[ inline]
108+ unsafe fn next ( & self , offset : Self :: Offset ) -> ( Self :: Offset , Option < Self :: Token > ) {
100109 if let Some ( tok) = self . get ( offset) {
101110 ( offset + 1 , Some ( tok. clone ( ) ) )
102111 } else {
112+ // We actually don't care if the offset goes beyond the end of the slice, and this seems to be *slightly* faster
103113 ( offset, None )
104114 }
105115 }
106116
117+ #[ inline]
107118 fn span ( & self , range : Range < Self :: Offset > ) -> Self :: Span {
108119 range. into ( )
109120 }
@@ -114,9 +125,12 @@ impl StrInput<u8> for [u8] {}
114125impl < T : Clone > SliceInput for [ T ] {
115126 type Slice = [ T ] ;
116127
128+ #[ inline]
117129 fn slice ( & self , range : Range < Self :: Offset > ) -> & Self :: Slice {
118130 & self [ range]
119131 }
132+
133+ #[ inline]
120134 fn slice_from ( & self , from : RangeFrom < Self :: Offset > ) -> & Self :: Slice {
121135 & self [ from]
122136 }
@@ -135,7 +149,7 @@ impl<'a, Ctx: Clone, I: Input + ?Sized> Input for WithContext<'a, Ctx, I> {
135149 self . 1 . start ( )
136150 }
137151
138- fn next ( & self , offset : Self :: Offset ) -> ( Self :: Offset , Option < Self :: Token > ) {
152+ unsafe fn next ( & self , offset : Self :: Offset ) -> ( Self :: Offset , Option < Self :: Token > ) {
139153 self . 1 . next ( offset)
140154 }
141155
@@ -228,13 +242,13 @@ impl<'a, 'parse, I: Input + ?Sized, E: ParserExtra<'a, I>> InputRef<'a, 'parse,
228242 }
229243
230244 /// Get the input offset that is currently being pointed to.
231- #[ inline( always ) ]
245+ #[ inline]
232246 pub fn offset ( & self ) -> I :: Offset {
233247 self . offset
234248 }
235249
236250 /// Save off a [`Marker`] to the current position in the input
237- #[ inline( always ) ]
251+ #[ inline]
238252 pub fn save ( & self ) -> Marker < I > {
239253 Marker {
240254 offset : self . offset ,
@@ -243,30 +257,31 @@ impl<'a, 'parse, I: Input + ?Sized, E: ParserExtra<'a, I>> InputRef<'a, 'parse,
243257 }
244258
245259 /// Reset the input state to the provided [`Marker`]
246- #[ inline( always ) ]
260+ #[ inline]
247261 pub fn rewind ( & mut self , marker : Marker < I > ) {
248262 self . errors . truncate ( marker. err_count ) ;
249263 self . offset = marker. offset ;
250264 }
251265
252- #[ inline( always ) ]
266+ #[ inline]
253267 pub ( crate ) fn state ( & mut self ) -> & mut E :: State {
254268 match & mut self . state {
255269 Ok ( state) => * state,
256270 Err ( state) => state,
257271 }
258272 }
259273
260- #[ inline( always ) ]
274+ #[ inline]
261275 pub ( crate ) fn ctx ( & self ) -> & E :: Context {
262276 & self . ctx
263277 }
264278
265- #[ inline( always ) ]
279+ #[ inline]
266280 pub ( crate ) fn skip_while < F : FnMut ( & I :: Token ) -> bool > ( & mut self , mut f : F ) {
267281 let mut offs = self . offset ;
268282 loop {
269- let ( offset, token) = self . input . next ( offs) ;
283+ // SAFETY: offset was generated by previous call to `Input::next`
284+ let ( offset, token) = unsafe { self . input . next ( offs) } ;
270285 if token. filter ( & mut f) . is_none ( ) {
271286 self . offset = offs;
272287 break ;
@@ -276,9 +291,10 @@ impl<'a, 'parse, I: Input + ?Sized, E: ParserExtra<'a, I>> InputRef<'a, 'parse,
276291 }
277292 }
278293
279- #[ inline( always ) ]
294+ #[ inline]
280295 pub ( crate ) fn next ( & mut self ) -> ( I :: Offset , Option < I :: Token > ) {
281- let ( offset, token) = self . input . next ( self . offset ) ;
296+ // SAFETY: offset was generated by previous call to `Input::next`
297+ let ( offset, token) = unsafe { self . input . next ( self . offset ) } ;
282298 self . offset = offset;
283299 ( self . offset , token)
284300 }
@@ -290,32 +306,33 @@ impl<'a, 'parse, I: Input + ?Sized, E: ParserExtra<'a, I>> InputRef<'a, 'parse,
290306
291307 /// Peek the next token in the input. Returns `None` for EOI
292308 pub fn peek ( & self ) -> Option < I :: Token > {
293- self . input . next ( self . offset ) . 1
309+ // SAFETY: offset was generated by previous call to `Input::next`
310+ unsafe { self . input . next ( self . offset ) . 1 }
294311 }
295312
296313 /// Skip the next token in the input.
297- #[ inline( always ) ]
314+ #[ inline]
298315 pub fn skip ( & mut self ) {
299316 let _ = self . next ( ) ;
300317 }
301318
302- #[ inline( always ) ]
319+ #[ inline]
303320 pub ( crate ) fn slice ( & self , range : Range < I :: Offset > ) -> & ' a I :: Slice
304321 where
305322 I : SliceInput ,
306323 {
307324 self . input . slice ( range)
308325 }
309326
310- #[ inline( always ) ]
327+ #[ inline]
311328 pub ( crate ) fn slice_from ( & self , from : RangeFrom < I :: Offset > ) -> & ' a I :: Slice
312329 where
313330 I : SliceInput ,
314331 {
315332 self . input . slice_from ( from)
316333 }
317334
318- #[ inline( always ) ]
335+ #[ inline]
319336 pub ( crate ) fn slice_trailing ( & self ) -> & ' a I :: Slice
320337 where
321338 I : SliceInput ,
@@ -324,12 +341,12 @@ impl<'a, 'parse, I: Input + ?Sized, E: ParserExtra<'a, I>> InputRef<'a, 'parse,
324341 }
325342
326343 /// Return the span from the provided [`Marker`] to the current position
327- #[ inline( always ) ]
344+ #[ inline]
328345 pub fn span_since ( & self , before : I :: Offset ) -> I :: Span {
329346 self . input . span ( before..self . offset )
330347 }
331348
332- #[ inline( always ) ]
349+ #[ inline]
333350 pub ( crate ) fn skip_bytes < C > ( & mut self , skip : usize )
334351 where
335352 C : Char ,
@@ -338,7 +355,7 @@ impl<'a, 'parse, I: Input + ?Sized, E: ParserExtra<'a, I>> InputRef<'a, 'parse,
338355 self . offset += skip;
339356 }
340357
341- #[ inline( always ) ]
358+ #[ inline]
342359 pub ( crate ) fn emit ( & mut self , error : E :: Error ) {
343360 self . errors . push ( error) ;
344361 }
0 commit comments