proclet 0.3.0

Proc macros made easy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
use crate::{
    Error, IntoTokens, Match, Span, ToTokenStream, ToTokens, TokenStreamExt, TokenTree,
    TokenTreeExt,
};
use std::{
    borrow::{Borrow, BorrowMut, Cow},
    marker::PhantomData,
    mem::transmute,
    ops::{
        Deref, DerefMut, Index, IndexMut, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo,
        RangeToInclusive,
    },
    slice,
};

/// Parse from a `TokenBuf`.
pub trait Parse<T: TokenTreeExt>:
    Sized + DefaultParser<T, Parser = DefaultParserImpl<T, Self>>
{
    /// Parse a value from a `TokenBuf`.
    ///
    /// The referenced `&TokenBuf` will be modified to point past the parsed tokens on success.
    fn parse(buf: &mut &TokenBuf<T>) -> Result<Self, Error<T::Span>>;

    /// Parse a value from a `TokenBuf` buffer, but return an error if there's any tokens left
    /// in the buffer after parsing.
    ///
    /// The referenced `&TokenBuf` will be modified to point past the parsed tokens on success.
    /// If parsing succeeds but there are tokens left in the buffer, the buffer will point
    /// to the left over tokens.
    #[inline]
    fn parse_all(buf: &mut &TokenBuf<T>) -> Result<Self, Error<T::Span>> {
        Self::parser().parse_all(buf)
    }
}

impl<T: TokenTreeExt, const LENGTH: usize> Parse<T> for [T; LENGTH] {
    #[inline]
    fn parse(buf: &mut &TokenBuf<T>) -> Result<Self, Error<T::Span>> {
        // can't use MaybeUninit for array init as rust claims the size is unknown when transmuting it
        if buf.len() >= LENGTH {
            let parsed = buf[..LENGTH]
                .into_iter()
                .cloned()
                .collect::<Vec<T>>()
                .try_into()
                .unwrap();
            *buf = &buf[LENGTH..];
            Ok(parsed)
        } else {
            Err(buf.error("no match"))
        }
    }
}

impl<T: TokenTreeExt, const LENGTH: usize> Parse<T> for Box<[T; LENGTH]> {
    #[inline]
    fn parse(buf: &mut &TokenBuf<T>) -> Result<Self, Error<T::Span>> {
        if buf.len() >= LENGTH {
            let parsed = buf[..LENGTH]
                .into_iter()
                .cloned()
                .collect::<Vec<T>>()
                .try_into()
                .unwrap();
            *buf = &buf[LENGTH..];
            Ok(parsed)
        } else {
            Err(buf.error("no match"))
        }
    }
}

impl<T: TokenTreeExt, X: Parse<T>> Parse<T> for Option<X> {
    #[inline]
    fn parse(buf: &mut &TokenBuf<T>) -> Result<Self, Error<T::Span>> {
        Ok(X::parse(buf).ok())
    }
}

impl<T: TokenTreeExt, X: Parse<T>> Parse<T> for Vec<X> {
    /// Parse a non-empty vector of items. If you want to accept an empty vector, use `Option<Vec<...>>::parse`.
    #[inline]
    fn parse(buf: &mut &TokenBuf<T>) -> Result<Self, Error<T::Span>> {
        let mut vec = Vec::new();
        while let Ok(item) = X::parse(buf) {
            vec.push(item);
        }
        if vec.is_empty() {
            Err(buf.error("no match"))
        } else {
            Ok(vec)
        }
    }
}

/// A parser for parsing values from a `TokenBuf`.
pub trait Parser<T: TokenTreeExt> {
    /// The output type of this parser.
    type Output<'p, 'b>
    where
        Self: 'p;

    /// Parse a value from a `TokenBuf` using this parser.
    ///
    /// The referenced `&TokenBuf` will be modified to point past the parsed tokens on success.
    fn parse<'p, 'b>(
        &'p self,
        buf: &mut &'b TokenBuf<T>,
    ) -> Result<Self::Output<'p, 'b>, Error<T::Span>>;

    /// Parse a value from a `TokenBuf` buffer, but return an error if there's any tokens left
    /// in the buffer after parsing.
    ///
    /// The referenced `&TokenBuf` will be modified to point past the parsed tokens on success.
    /// If parsing succeeds but there are tokens left in the buffer, the buffer will point
    /// to the left over tokens.
    #[inline]
    fn parse_all<'p, 'b>(
        &'p self,
        buf: &mut &'b TokenBuf<T>,
    ) -> Result<Self::Output<'p, 'b>, Error<T::Span>> {
        match self.parse(buf) {
            Ok(result) if buf.is_empty() => Ok(result),
            Err(e) => Err(e),
            _ => Err(buf.error("unexpected tokens after input")),
        }
    }

    /// Wrap this parser in [`Optional`] to make it always succeed and return an option.
    #[inline]
    fn optional(self) -> Optional<Self>
    where
        Self: Sized,
    {
        Optional(self)
    }
}

impl<T: TokenTreeExt, X: Parser<T>> Parser<T> for [X] {
    type Output<'p, 'b> = Vec<X::Output<'p, 'b>> where Self: 'p;

    #[inline]
    fn parse<'p, 'b>(
        &'p self,
        buf: &mut &'b TokenBuf<T>,
    ) -> Result<Self::Output<'p, 'b>, Error<T::Span>> {
        self.iter().map(|x| x.parse(buf)).collect()
    }
}

/// Trait for making a default parser. This is automatically implemented for objects
/// that implement the `Parse` trait.
pub trait DefaultParser<T: TokenTreeExt> {
    /// The parser that will be created.
    type Parser: Parser<T> + Copy + Default;

    /// Create a new parser.
    #[inline(always)]
    fn parser() -> Self::Parser {
        Self::Parser::default()
    }
}

impl<T: TokenTreeExt, X: Parse<T>> DefaultParser<T> for X {
    type Parser = DefaultParserImpl<T, X>;
}

/// Wrap a parser in this to make it always succeed and return an `Option`.
#[repr(transparent)]
pub struct Optional<T>(pub T);

impl<T: TokenTreeExt, X: Parser<T>> Parser<T> for Optional<X> {
    type Output<'p, 'b> = Option<X::Output<'p, 'b>> where Self: 'p;

    #[inline]
    fn parse<'p, 'b>(
        &'p self,
        buf: &mut &'b TokenBuf<T>,
    ) -> Result<Self::Output<'p, 'b>, Error<T::Span>> {
        Ok(self.0.parse(buf).ok())
    }
}

pub struct DefaultParserImpl<T: TokenTreeExt, X: Parse<T>>(PhantomData<fn() -> (T, X)>);

impl<T: TokenTreeExt, X: Parse<T>> Clone for DefaultParserImpl<T, X> {
    #[inline(always)]
    fn clone(&self) -> Self {
        *self
    }
}

impl<T: TokenTreeExt, X: Parse<T>> Copy for DefaultParserImpl<T, X> {}

impl<T: TokenTreeExt, X: Parse<T>> Default for DefaultParserImpl<T, X> {
    #[inline(always)]
    fn default() -> Self {
        Self(PhantomData)
    }
}

impl<T: TokenTreeExt, X: Parse<T>> Parser<T> for DefaultParserImpl<T, X> {
    type Output<'p, 'b> = X where Self: 'p;

    #[inline]
    fn parse<'p, 'b>(
        &'p self,
        buf: &mut &'b TokenBuf<T>,
    ) -> Result<Self::Output<'p, 'b>, Error<T::Span>> {
        X::parse(buf)
    }
}

/// Methods for making or extending a `TokenBuffer` with tokens representing this object.
/// This is automatically implemented for types that implement the [`IntoTokens`] trait.
pub trait ToTokenBuffer<T: TokenTree> {
    /// Extend the given `TokenBuffer` with tokens representing this object.
    fn extend_token_buffer(&self, token_buffer: &mut TokenBuffer<T>);

    /// Make a new `TokenBuffer` with tokens representing this object.
    #[inline]
    fn to_token_buffer(&self) -> TokenBuffer<T> {
        let mut tb = TokenBuffer::new();
        self.extend_token_buffer(&mut tb);
        tb
    }
}

impl<T: TokenTree, X: IntoTokens<T> + Clone> ToTokenBuffer<T> for X {
    #[inline]
    fn extend_token_buffer(&self, token_buffer: &mut TokenBuffer<T>) {
        token_buffer.0.extend(self.to_tokens())
    }
}

/// Automatically implemented for types that implement `Into<&TokenBuf>` for `&Type`.
pub trait AsTokenBuf<'a, T: TokenTree> {
    /// Get a reference to this as a `TokenBuf`.
    fn as_token_buf(&'a self) -> &'a TokenBuf<T>;
}

impl<'a, T: TokenTree, X: 'a> AsTokenBuf<'a, T> for X
where
    &'a X: Into<&'a TokenBuf<T>>,
{
    #[inline]
    fn as_token_buf(&'a self) -> &'a TokenBuf<T> {
        self.into()
    }
}

/// Automatically implemented for types that implement `Into<&mut TokenBuf>` for `&mut Type`.
pub trait AsTokenBufMut<'a, T: TokenTree> {
    /// Get a mutable reference to this as a `TokenBuf`.
    fn as_token_buf_mut(&'a mut self) -> &'a mut TokenBuf<T>;
}

impl<'a, T: TokenTree, X: 'a> AsTokenBufMut<'a, T> for X
where
    &'a mut X: Into<&'a mut TokenBuf<T>>,
{
    #[inline]
    fn as_token_buf_mut(&'a mut self) -> &'a mut TokenBuf<T> {
        self.into()
    }
}

/// An owned buffer of tokens.
#[derive(Clone, Debug, Default)]
pub struct TokenBuffer<T: TokenTree>(Vec<T>);

impl<T: TokenTreeExt> TokenBuffer<T> {
    /// Get this buffer as a `&TokenBuf`.
    #[inline]
    pub fn as_buf(&self) -> &TokenBuf<T> {
        self
    }

    /// Get this buffer as a `&mut TokenBuf`.
    #[inline]
    pub fn as_buf_mut(&mut self) -> &mut TokenBuf<T> {
        self
    }

    /// Parse a value from this buffer, but return an error if there's any tokens left in the
    /// buffer after parsing.
    ///
    /// Unlike `TokenBuf::parse_all`, this doesn't modify the reference to self.
    #[inline]
    pub fn parse_all<P: Parse<T>>(&self) -> Result<P, Error<T::Span>> {
        self.as_buf().parse_all() // rust-analyzer incorrectly marks this as an error
    }
}

impl<T: TokenTree> TokenBuffer<T> {
    /// Create a new `TokenBuffer`.
    #[inline]
    pub const fn new() -> Self {
        Self(Vec::new())
    }
}

impl<T: TokenTreeExt> AsRef<TokenBuf<T>> for TokenBuffer<T> {
    #[inline]
    fn as_ref(&self) -> &TokenBuf<T> {
        self.as_buf()
    }
}

impl<T: TokenTreeExt> AsMut<TokenBuf<T>> for TokenBuffer<T> {
    #[inline]
    fn as_mut(&mut self) -> &mut TokenBuf<T> {
        self.as_buf_mut()
    }
}

impl<T: TokenTreeExt> Borrow<TokenBuf<T>> for TokenBuffer<T> {
    #[inline]
    fn borrow(&self) -> &TokenBuf<T> {
        self.as_buf()
    }
}

impl<T: TokenTreeExt> BorrowMut<TokenBuf<T>> for TokenBuffer<T> {
    #[inline]
    fn borrow_mut(&mut self) -> &mut TokenBuf<T> {
        self.as_buf_mut()
    }
}

impl<T: TokenTreeExt> Deref for TokenBuffer<T> {
    type Target = TokenBuf<T>;

    #[inline]
    fn deref(&self) -> &Self::Target {
        TokenBuf::from_ref(&self.0[..])
    }
}

impl<T: TokenTreeExt> DerefMut for TokenBuffer<T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        TokenBuf::from_mut(&mut self.0[..])
    }
}

impl<T: TokenTree, X: ToTokenBuffer<T>> Extend<X> for TokenBuffer<T> {
    #[inline]
    fn extend<I: IntoIterator<Item = X>>(&mut self, iter: I) {
        for i in iter {
            i.extend_token_buffer(self);
        }
    }
}

#[cfg(feature = "proc-macro")]
impl From<proc_macro::TokenStream> for TokenBuffer<proc_macro::TokenTree> {
    #[inline]
    fn from(value: proc_macro::TokenStream) -> Self {
        Self::from_iter(value)
    }
}

#[cfg(feature = "proc-macro2")]
impl From<proc_macro2::TokenStream> for TokenBuffer<proc_macro2::TokenTree> {
    #[inline]
    fn from(value: proc_macro2::TokenStream) -> Self {
        Self::from_iter(value)
    }
}

#[cfg(feature = "proc-macro")]
impl From<TokenBuffer<proc_macro::TokenTree>> for proc_macro::TokenStream {
    #[inline]
    fn from(value: TokenBuffer<proc_macro::TokenTree>) -> Self {
        value.to_token_stream()
    }
}

#[cfg(feature = "proc-macro2")]
impl From<TokenBuffer<proc_macro2::TokenTree>> for proc_macro2::TokenStream {
    #[inline]
    fn from(value: TokenBuffer<proc_macro2::TokenTree>) -> Self {
        value.to_token_stream()
    }
}

impl<T: TokenTree> From<TokenBuffer<T>> for Box<[T]> {
    #[inline]
    fn from(value: TokenBuffer<T>) -> Self {
        value.0.into()
    }
}

impl<T: TokenTree> From<TokenBuffer<T>> for Vec<T> {
    #[inline]
    fn from(value: TokenBuffer<T>) -> Self {
        value.0
    }
}

impl<T: TokenTree> From<Vec<T>> for TokenBuffer<T> {
    #[inline]
    fn from(value: Vec<T>) -> Self {
        Self(value)
    }
}

impl<T: TokenTree, X: ToTokenBuffer<T>> FromIterator<X> for TokenBuffer<T> {
    #[inline]
    fn from_iter<I: IntoIterator<Item = X>>(iter: I) -> Self {
        let mut buf = TokenBuffer::new();
        for i in iter {
            i.extend_token_buffer(&mut buf);
        }
        buf
    }
}

impl<T: TokenTree, I: TokenBufferIndex<T>> Index<I> for TokenBuffer<T> {
    type Output = I::Output;

    #[inline]
    fn index(&self, index: I) -> &Self::Output {
        index.index(&self.0)
    }
}

impl<T: TokenTree, I: TokenBufferIndex<T>> IndexMut<I> for TokenBuffer<T> {
    #[inline]
    fn index_mut(&mut self, index: I) -> &mut Self::Output {
        index.index_mut(&mut self.0)
    }
}

impl<T: TokenTree> IntoIterator for TokenBuffer<T> {
    type IntoIter = <Vec<T> as IntoIterator>::IntoIter;
    type Item = T;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl<T: TokenTree> IntoTokens<T> for TokenBuffer<T> {
    #[inline]
    fn into_tokens(self) -> impl Iterator<Item = T>
    where
        Self: Sized,
    {
        self.0.into_iter()
    }
}

impl<T: TokenTree, const LENGTH: usize> TryFrom<TokenBuffer<T>> for [T; LENGTH] {
    type Error = <Self as TryFrom<Vec<T>>>::Error;

    #[inline]
    fn try_from(value: TokenBuffer<T>) -> Result<Self, Self::Error> {
        value.0.try_into()
    }
}

/// Borrowed version of [`TokenBuffer`].
#[derive(Debug)]
#[repr(transparent)]
pub struct TokenBuf<T: TokenTree>([T]);

impl<T: TokenTreeExt> TokenBuf<T> {
    #[inline]
    fn from_ref(r: &[T]) -> &Self {
        unsafe {
            // # Safety
            // It's a reference to the same type in a transparent struct
            transmute::<&[T], &Self>(r)
        }
    }

    #[inline]
    fn from_mut(r: &mut [T]) -> &mut Self {
        unsafe {
            // # Safety
            // It's a reference to the same type in a transparent struct
            transmute::<&mut [T], &mut Self>(r)
        }
    }

    /// Create an [`Error`] spanned to the beginning of this buffer, or with the default span
    /// if the buffer is empty.
    #[inline]
    pub fn error(&self, message: impl Into<Cow<'static, str>>) -> Error<T::Span> {
        Error::with_span(
            self.0
                .first()
                .map(|t| t.span())
                .unwrap_or_else(T::Span::call_site),
            message,
        )
    }

    /// Parse a value from this buffer.
    ///
    /// The referenced `&self` will be modified to point past the parsed tokens on success.
    #[inline]
    pub fn parse<P: Parse<T>>(self: &mut &Self) -> Result<P, Error<T::Span>> {
        P::parse(self)
    }

    /// Parse a value from this buffer, but return an error if there's any tokens left in the
    /// buffer after parsing.
    ///
    /// The referenced `&self` will be modified to point past the parsed tokens on success.
    /// If parsing succeeds but there are tokens left in the buffer, the buffer will point
    /// to the left over tokens.
    #[inline]
    pub fn parse_all<P: Parse<T>>(self: &mut &Self) -> Result<P, Error<T::Span>> {
        P::parse_all(self)
    }

    /// Parse a prefix from this buffer. `match_fn` is called for each token in the buffer
    /// starting from the beginning. Parsing stops if `match_fn` returns `Match::Complete`
    /// or `Match::NoMatch`, or if the buffer runs out of tokens.
    ///
    /// The referenced `&self` will be modified to point past the parsed tokens on success.
    #[inline]
    pub fn parse_prefix<'a, M: 'a>(
        self: &mut &'a Self,
        mut match_fn: impl FnMut(&T) -> Match<M>,
    ) -> Result<M, Error<T::Span>> {
        self.parse_prefix_buf(|_, token, _| match_fn(token))
    }

    /// Parse a prefix from this buffer. `match_fn` is called for each token in the buffer
    /// starting from the beginning. Parsing stops if `match_fn` returns `Match::Complete`
    /// or `Match::NoMatch`, or if the buffer runs out of tokens.
    ///
    /// `match_fn` is called with the token at the current position and the next token, if any.
    ///
    /// The referenced `&self` will be modified to point past the parsed tokens on success.
    #[inline]
    pub fn parse_prefix_next<'a, M: 'a>(
        self: &mut &'a Self,
        mut match_fn: impl FnMut(&T, Option<&T>) -> Match<M>,
    ) -> Result<M, Error<T::Span>> {
        self.parse_prefix_buf(|_, token, next| match_fn(token, next))
    }

    /// Parse a prefix from this buffer. `match_fn` is called for each token in the buffer
    /// starting from the beginning. Parsing stops if `match_fn` returns `Match::Complete`
    /// or `Match::NoMatch`, or if the buffer runs out of tokens.
    ///
    /// `match_fn` is called with the currently matched buffer including the current token,
    /// the token at the current position, and the next token, if any.
    ///
    /// The referenced `&self` will be modified to point past the parsed tokens on success.
    #[inline]
    pub fn parse_prefix_buf<'a, M: 'a>(
        self: &mut &'a Self,
        mut match_fn: impl FnMut(&'a Self, &T, Option<&T>) -> Match<M>,
    ) -> Result<M, Error<T::Span>> {
        let mut result = None;
        for i in 1..=self.len() {
            match match_fn(&self[..i], &self[i - 1], self.get(i)) {
                Match::Complete(m) => {
                    *self = &self[i..];
                    return Ok(m);
                }
                Match::Partial(m) => result = Some((m, &self[i..])),
                Match::NeedMore => (),
                Match::NoMatch => break,
            }
        }
        result
            .ok_or_else(|| self.error("no match"))
            .map(|(result, rest)| {
                *self = rest;
                result
            })
    }

    /// Parse a suffix from this buffer. `match_fn` is called for each token in the buffer
    /// starting from the end. Parsing stops if `match_fn` returns `Match::Complete`
    /// or `Match::NoMatch`, or if the buffer runs out of tokens.
    ///
    /// The referenced `&self` will be modified to end before the parsed tokens on success.
    #[inline]
    pub fn parse_suffix<'a, M: 'a>(
        self: &mut &'a Self,
        mut match_fn: impl FnMut(&T) -> Match<M>,
    ) -> Result<M, Error<T::Span>> {
        self.parse_suffix_buf(|_, token, _| match_fn(token))
    }

    /// Parse a suffix from this buffer. `match_fn` is called for each token in the buffer
    /// starting from the end. Parsing stops if `match_fn` returns `Match::Complete`
    /// or `Match::NoMatch`, or if the buffer runs out of tokens.
    ///
    /// `match_fn` is called with the token at the current position and the preceding token, if any.
    ///
    /// The referenced `&self` will be modified to end before the parsed tokens on success.
    #[inline]
    pub fn parse_suffix_next<'a, M: 'a>(
        self: &mut &'a Self,
        mut match_fn: impl FnMut(&T, Option<&T>) -> Match<M>,
    ) -> Result<M, Error<T::Span>> {
        self.parse_suffix_buf(|_, token, next| match_fn(token, next))
    }

    /// Parse a suffix from this buffer. `match_fn` is called for each token in the buffer
    /// starting from the end. Parsing stops if `match_fn` returns `Match::Complete`
    /// or `Match::NoMatch`, or if the buffer runs out of tokens.
    ///
    /// `match_fn` is called with the currently matched buffer including the current token,
    /// the token at the current position, and the preceding token, if any.
    ///
    /// The referenced `&self` will be modified to end before the parsed tokens on success.
    #[inline]
    pub fn parse_suffix_buf<'a, M: 'a>(
        self: &mut &'a Self,
        mut match_fn: impl FnMut(&'a Self, &T, Option<&T>) -> Match<M>,
    ) -> Result<M, Error<T::Span>> {
        let mut result = None;
        for i in (0..self.len()).rev() {
            match match_fn(&self[i..], &self[i], (i > 0).then(|| &self[i - 1])) {
                Match::Complete(m) => {
                    *self = &self[..i];
                    return Ok(m);
                }
                Match::Partial(m) => result = Some((m, &self[..i])),
                Match::NeedMore => (),
                Match::NoMatch => break,
            }
        }
        result
            .ok_or_else(|| self.error("no match"))
            .map(|(result, rest)| {
                *self = rest;
                result
            })
    }
}

impl<T: TokenTree> Deref for TokenBuf<T> {
    type Target = [T];

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T: TokenTree> DerefMut for TokenBuf<T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

#[cfg(feature = "proc-macro")]
impl From<&TokenBuf<proc_macro::TokenTree>> for proc_macro::TokenStream {
    #[inline]
    fn from(value: &TokenBuf<proc_macro::TokenTree>) -> Self {
        value.to_token_stream()
    }
}

#[cfg(feature = "proc-macro")]
impl From<&mut TokenBuf<proc_macro::TokenTree>> for proc_macro::TokenStream {
    #[inline]
    fn from(value: &mut TokenBuf<proc_macro::TokenTree>) -> Self {
        value.to_token_stream()
    }
}

#[cfg(feature = "proc-macro2")]
impl From<&TokenBuf<proc_macro2::TokenTree>> for proc_macro2::TokenStream {
    #[inline]
    fn from(value: &TokenBuf<proc_macro2::TokenTree>) -> Self {
        value.to_token_stream()
    }
}

#[cfg(feature = "proc-macro2")]
impl From<&mut TokenBuf<proc_macro2::TokenTree>> for proc_macro2::TokenStream {
    #[inline]
    fn from(value: &mut TokenBuf<proc_macro2::TokenTree>) -> Self {
        value.to_token_stream()
    }
}

impl<'a, T: TokenTreeExt> From<&'a TokenBuffer<T>> for &'a TokenBuf<T> {
    #[inline]
    fn from(value: &'a TokenBuffer<T>) -> Self {
        value.as_buf()
    }
}

impl<'a, T: TokenTreeExt> From<&'a mut TokenBuffer<T>> for &'a mut TokenBuf<T> {
    #[inline]
    fn from(value: &'a mut TokenBuffer<T>) -> Self {
        value.as_buf_mut()
    }
}

impl<'a, T: TokenTreeExt> From<&'a [T]> for &'a TokenBuf<T> {
    #[inline]
    fn from(value: &'a [T]) -> Self {
        TokenBuf::from_ref(value)
    }
}

impl<'a, T: TokenTreeExt> From<&'a mut [T]> for &'a mut TokenBuf<T> {
    #[inline]
    fn from(value: &'a mut [T]) -> Self {
        TokenBuf::from_mut(value)
    }
}

impl<T: TokenTree, I: TokenBufferIndex<T>> Index<I> for TokenBuf<T> {
    type Output = I::Output;

    #[inline]
    fn index(&self, index: I) -> &Self::Output {
        index.index(&self.0)
    }
}

impl<T: TokenTree, I: TokenBufferIndex<T>> IndexMut<I> for TokenBuf<T> {
    #[inline]
    fn index_mut(&mut self, index: I) -> &mut Self::Output {
        index.index_mut(&mut self.0)
    }
}

impl<'a, T: TokenTree> IntoIterator for &'a TokenBuf<T> {
    type IntoIter = slice::Iter<'a, T>;
    type Item = &'a T;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.0.iter()
    }
}

impl<'a, T: TokenTree> IntoIterator for &'a mut TokenBuf<T> {
    type IntoIter = slice::IterMut<'a, T>;
    type Item = &'a mut T;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.0.iter_mut()
    }
}

impl<T: TokenTreeExt> ToOwned for TokenBuf<T> {
    type Owned = TokenBuffer<T>;

    #[inline]
    fn to_owned(&self) -> Self::Owned {
        TokenBuffer(self.0.to_vec())
    }
}

impl<T: TokenTree> ToTokens<T> for TokenBuf<T> {
    #[inline]
    fn to_tokens(&self) -> impl Iterator<Item = T> {
        // to_owned doesn't work here bc rust
        TokenBuffer::<T>(self.0.to_vec()).into_tokens()
    }
}

impl<T: TokenStreamExt> ToTokenStream<T> for TokenBuf<T::TokenTree> {
    #[inline]
    fn extend_token_stream(&self, token_stream: &mut T) {
        for i in self.0.iter() {
            i.extend_token_stream(token_stream);
        }
    }
}

pub trait TokenBufferIndex<T: TokenTree> {
    type Output: ?Sized;
    fn index(self, slice: &[T]) -> &Self::Output;
    fn index_mut(self, slice: &mut [T]) -> &mut Self::Output;
}

impl<T: TokenTree> TokenBufferIndex<T> for usize {
    type Output = T;

    #[inline]
    fn index(self, slice: &[T]) -> &Self::Output {
        &slice[self]
    }

    #[inline]
    fn index_mut(self, slice: &mut [T]) -> &mut Self::Output {
        &mut slice[self]
    }
}

impl<T: TokenTreeExt> TokenBufferIndex<T> for Range<usize> {
    type Output = TokenBuf<T>;

    #[inline]
    fn index(self, slice: &[T]) -> &Self::Output {
        TokenBuf::from_ref(&slice[self.start..self.end])
    }

    #[inline]
    fn index_mut(self, slice: &mut [T]) -> &mut Self::Output {
        TokenBuf::from_mut(&mut slice[self.start..self.end])
    }
}

impl<T: TokenTreeExt> TokenBufferIndex<T> for RangeFrom<usize> {
    type Output = TokenBuf<T>;

    #[inline]
    fn index(self, slice: &[T]) -> &Self::Output {
        TokenBuf::from_ref(&slice[self.start..])
    }

    #[inline]
    fn index_mut(self, slice: &mut [T]) -> &mut Self::Output {
        TokenBuf::from_mut(&mut slice[self.start..])
    }
}

impl<T: TokenTreeExt> TokenBufferIndex<T> for RangeFull {
    type Output = TokenBuf<T>;

    #[inline]
    fn index(self, slice: &[T]) -> &Self::Output {
        TokenBuf::from_ref(slice)
    }

    #[inline]
    fn index_mut(self, slice: &mut [T]) -> &mut Self::Output {
        TokenBuf::from_mut(slice)
    }
}

impl<T: TokenTreeExt> TokenBufferIndex<T> for RangeInclusive<usize> {
    type Output = TokenBuf<T>;

    #[inline]
    fn index(self, slice: &[T]) -> &Self::Output {
        TokenBuf::from_ref(&slice[*self.start()..=*self.end()])
    }

    #[inline]
    fn index_mut(self, slice: &mut [T]) -> &mut Self::Output {
        TokenBuf::from_mut(&mut slice[*self.start()..=*self.end()])
    }
}

impl<T: TokenTreeExt> TokenBufferIndex<T> for RangeTo<usize> {
    type Output = TokenBuf<T>;

    #[inline]
    fn index(self, slice: &[T]) -> &Self::Output {
        TokenBuf::from_ref(&slice[..self.end])
    }

    #[inline]
    fn index_mut(self, slice: &mut [T]) -> &mut Self::Output {
        TokenBuf::from_mut(&mut slice[..self.end])
    }
}

impl<T: TokenTreeExt> TokenBufferIndex<T> for RangeToInclusive<usize> {
    type Output = TokenBuf<T>;

    #[inline]
    fn index(self, slice: &[T]) -> &Self::Output {
        TokenBuf::from_ref(&slice[..=self.end])
    }

    #[inline]
    fn index_mut(self, slice: &mut [T]) -> &mut Self::Output {
        TokenBuf::from_mut(&mut slice[..=self.end])
    }
}