met 0.2.0

Macros and extension traits for proc-macro2
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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
//! **Met** provides several macros and extension traits that make working with
//! [`proc-macro2`] easier.
//!
//! # Setup
//!
//! Add this to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! met = "0.2.0"
//! proc-macro2 = "1"
//! ```
//!
//! [`proc-macro2`]: https://crates.io/crates/proc-macro2

use std::ffi::CStr;

use proc_macro2::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};

mod sealed {
    pub trait Sealed {}
}

/// [`TokenStream`] quasi-quoting macro.
///
/// This is not a replacement for [`quote::quote!`]. It's slower and does not
/// support variable interpolation. It is intended for cases where
/// interpolation must be avoided entirely.
///
/// # Examples
///
/// ```
/// # use met::assert_stream_eq;
/// use met::stream;
/// use quote::quote;
///
/// let a = stream! {
///     impl MyTrait for MyStruct {}
/// };
///
/// let b = quote! {
///     impl MyTrait for MyStruct {}
/// };
///
/// assert_stream_eq!(a, b);
/// ```
///
/// When `#` is followed by an ident, `quote!` performs variable
/// interpolation, but `stream!` does not.
///
/// ```
/// # use met::assert_stream_ne;
/// use met::stream;
/// use quote::quote;
///
/// let variable = 5;
///
/// let a = stream! { # variable };
/// let b = quote! { # variable };
///
/// // Not equal, `stream!` doesn't interpolate!
/// assert_stream_ne!(a, b);
/// ```
///
/// [`quote::quote!`]: https://docs.rs/quote/latest/quote/macro.quote.html
#[macro_export]
macro_rules! stream {
    ($($tt:tt)*) => {
        stringify!($($tt)*)
            .parse::<::proc_macro2::TokenStream>()
            .expect("couldn't parse tokens")
    };
}

/// [`TokenTree`] quasi-quoting macro.
#[macro_export]
macro_rules! tree {
    ($tt:tt) => {
        $crate::stream!($tt)
            .into_iter()
            .next()
            .expect("expected a token")
    };
}

/// [`Group`] quasi-quoting macro.
///
/// # Examples
///
/// ```
/// # use met::{GroupExt, assert_stream_eq, stream};
/// use met::group;
///
/// let group = group! { [1, 2, 3] };
///
/// assert!(group.is_bracketed());
/// assert_stream_eq!(group.stream(), stream! { 1, 2, 3 });
/// ```
///
/// # Panics
///
/// Panics if the token isn't a group.
///
/// ```should_panic
/// use met::group;
///
/// group! { "met" };
/// ```
#[macro_export]
macro_rules! group {
    ($tt:tt) => {
        $crate::TokenTreeExt::into_group($crate::tree!($tt)).expect("expected a group")
    };
}

/// [`Ident`] quasi-quoting macro.
///
/// # Examples
///
/// ```
/// use met::ident;
///
/// let ident = ident! { variable };
///
/// assert_eq!(ident, "variable");
/// ```
///
/// # Panics
///
/// Panics if the token isn't an ident.
///
/// ```should_panic
/// use met::ident;
///
/// ident! { [1, 2, 3] };
/// ```
#[macro_export]
macro_rules! ident {
    ($tt:tt) => {
        $crate::TokenTreeExt::into_ident($crate::tree!($tt)).expect("expected an ident")
    };
}

/// [`Punct`] quasi-quoting macro.
///
/// # Examples
///
/// ```
/// # use met::PunctExt;
/// use met::punct;
///
/// let punct = punct! { < };
///
/// assert!(punct.is_char('<'));
/// assert!(punct.is_alone());
/// ```
///
/// # Panics
///
/// Panics if the token isn't a punct.
///
/// ```should_panic
/// use met::punct;
///
/// punct! { variable };
/// ```
#[macro_export]
macro_rules! punct {
    ($tt:tt) => {
        $crate::TokenTreeExt::into_punct($crate::tree!($tt)).expect("expected a punct")
    };
}

/// [`Literal`] quasi-quoting macro.
///
/// # Examples
///
/// ```
/// use met::literal;
///
/// let literal = literal! { "met" };
///
/// assert_eq!(literal.to_string(), "\"met\"");
/// ```
///
/// # Panics
///
/// Panics if the token isn't a literal.
///
/// ```should_panic
/// use met::literal;
///
/// literal! { < };
/// ```
#[macro_export]
macro_rules! literal {
    ($tt:tt) => {
        $crate::TokenTreeExt::into_literal($crate::tree!($tt)).expect("expected a literal")
    };
}

/// Asserts that two streams are equivalent.
#[macro_export]
macro_rules! assert_stream_eq {
    ($left:expr, $right:expr) => {
        assert!($crate::TokenStreamExt::equals(&$left, &$right));
    };
}

/// Asserts that two streams are not equivalent.
#[macro_export]
macro_rules! assert_stream_ne {
    ($left:expr, $right:expr) => {
        assert!(!$crate::TokenStreamExt::equals(&$left, &$right));
    };
}

/// Asserts that two trees are equivalent.
#[macro_export]
macro_rules! assert_tree_eq {
    ($left:expr, $right:expr) => {
        assert!($crate::TokenTreeExt::equals(&$left, &$right));
    };
}

/// Asserts that two trees are not equivalent.
#[macro_export]
macro_rules! assert_tree_ne {
    ($left:expr, $right:expr) => {
        assert!(!$crate::TokenTreeExt::equals(&$left, &$right));
    };
}

/// Asserts that two groups are equivalent.
#[macro_export]
macro_rules! assert_group_eq {
    ($left:expr, $right:expr) => {
        assert!($crate::GroupExt::equals(&$left, &$right));
    };
}

/// Asserts that two groups are not equivalent.
#[macro_export]
macro_rules! assert_group_ne {
    ($left:expr, $right:expr) => {
        assert!(!$crate::GroupExt::equals(&$left, &$right));
    };
}

/// Asserts that two idents are equivalent.
#[macro_export]
macro_rules! assert_ident_eq {
    ($left:expr, $right:expr) => {
        assert!($crate::IdentExt::equals(&$left, &$right));
    };
}

/// Asserts that two idents are not equivalent.
#[macro_export]
macro_rules! assert_ident_ne {
    ($left:expr, $right:expr) => {
        assert!(!$crate::IdentExt::equals(&$left, &$right));
    };
}

/// Asserts that two puncts are equivalent.
#[macro_export]
macro_rules! assert_punct_eq {
    ($left:expr, $right:expr) => {
        assert!($crate::PunctExt::equals(&$left, &$right));
    };
}

/// Asserts that two puncts are not equivalent.
#[macro_export]
macro_rules! assert_punct_ne {
    ($left:expr, $right:expr) => {
        assert!(!$crate::PunctExt::equals(&$left, &$right));
    };
}

/// Asserts that two literals are equivalent.
#[macro_export]
macro_rules! assert_literal_eq {
    ($left:expr, $right:expr) => {
        assert!($crate::LiteralExt::equals(&$left, &$right));
    };
}

/// Asserts that two literals are not equivalent.
#[macro_export]
macro_rules! assert_literal_ne {
    ($left:expr, $right:expr) => {
        assert!(!$crate::LiteralExt::equals(&$left, &$right));
    };
}

/// Extension trait for [`TokenStream`].
pub trait TokenStreamExt: Sized + sealed::Sealed {
    /// Construct a token stream from a single token.
    fn token(token: impl Into<TokenTree>) -> Self;

    /// Returns `true` if the token stream is equal to `other`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use met::stream;
    /// use met::TokenStreamExt;
    ///
    /// let a = stream! {
    ///     if true {
    ///         return 8;
    ///     }
    /// };
    ///
    /// let b = stream! {
    ///     if
    ///         true { return
    ///         8; }
    /// };
    ///
    /// // Equal, the tokens are identical!
    /// assert!(a.equals(&b));
    ///
    /// let c = stream! {
    ///     if true {
    ///         return 8
    ///     }
    /// };
    ///
    /// // Not equal, `c` is missing a semicolon (;) after `8`.
    /// assert!(!a.equals(&c));
    /// ```
    fn equals(&self, other: &Self) -> bool;

    /// Append a token to the token stream.
    ///
    /// # Examples
    ///
    /// ```
    /// # use met::{assert_stream_eq, stream};
    /// # use proc_macro2::{Ident, Literal, Punct, Spacing, Span, TokenStream};
    /// use met::TokenStreamExt;
    ///
    /// let mut expression = TokenStream::new();
    ///
    /// expression.append(Ident::new("variable", Span::call_site()));
    /// expression.append(Punct::new('=', Spacing::Joint));
    /// expression.append(Punct::new('=', Spacing::Alone));
    /// expression.append(Literal::isize_unsuffixed(100));
    ///
    /// assert_stream_eq!(expression, stream! { variable == 100 });
    /// ```
    fn append<T>(&mut self, token: T)
    where
        T: Into<TokenTree>;
}

impl TokenStreamExt for TokenStream {
    fn token(token: impl Into<TokenTree>) -> Self {
        Self::from(token.into())
    }

    fn equals(&self, other: &Self) -> bool {
        let mut this = self.clone().into_iter();
        let mut other = other.clone().into_iter();

        loop {
            match (this.next(), other.next()) {
                (Some(this), Some(other)) if this.equals(&other) => {}
                (None, None) => return true,
                _ => return false,
            }
        }
    }

    fn append<T>(&mut self, token: T)
    where
        T: Into<TokenTree>,
    {
        self.extend(Some(token.into()));
    }
}

impl sealed::Sealed for TokenStream {}

/// Extension trait for [`TokenTree`].
pub trait TokenTreeExt: Sized + sealed::Sealed {
    /// Returns `true` if this token tree is equal to `other`.
    fn equals(&self, other: &Self) -> bool;

    /// Returns `true` if the token tree is a group.
    fn is_group(&self) -> bool;

    /// Returns `true` if the token tree is an ident.
    fn is_ident(&self) -> bool;

    /// Returns `true` if the token tree is a punct.
    fn is_punct(&self) -> bool;

    /// Returns `true` if the token tree is a literal.
    fn is_literal(&self) -> bool;

    /// Require the token tree to be a group.
    fn into_group(self) -> Result<Group, Self>;

    /// Require the token tree to be an ident.
    fn into_ident(self) -> Result<Ident, Self>;

    /// Require the token tree to be a punct.
    fn into_punct(self) -> Result<Punct, Self>;

    /// Require the token tree to be a literal.
    fn into_literal(self) -> Result<Literal, Self>;

    /// Returns a reference to the group, if it is one.
    fn as_group(&self) -> Option<&Group>;

    /// Returns a reference to the ident, if it is one.
    fn as_ident(&self) -> Option<&Ident>;

    /// Returns a reference to the punct, if it is one.
    fn as_punct(&self) -> Option<&Punct>;

    /// Returns a reference to the literal, if it is one.
    fn as_literal(&self) -> Option<&Literal>;

    /// Returns a mutable reference to the group, if it is one.
    fn as_group_mut(&mut self) -> Option<&mut Group>;

    /// Returns a mutable reference to the ident, if it is one.
    fn as_ident_mut(&mut self) -> Option<&mut Ident>;

    /// Returns a mutable reference to the punct, if it is one.
    fn as_punct_mut(&mut self) -> Option<&mut Punct>;

    /// Returns a mutable reference to the literal, if it is one.
    fn as_literal_mut(&mut self) -> Option<&mut Literal>;
}

impl TokenTreeExt for TokenTree {
    fn is_group(&self) -> bool {
        matches!(self, Self::Group(_))
    }

    fn is_ident(&self) -> bool {
        matches!(self, Self::Ident(_))
    }

    fn is_punct(&self) -> bool {
        matches!(self, Self::Punct(_))
    }

    fn is_literal(&self) -> bool {
        matches!(self, Self::Literal(_))
    }

    fn equals(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Group(this), Self::Group(other)) => this.equals(other),
            (Self::Ident(this), Self::Ident(other)) => this.equals(other),
            (Self::Punct(this), Self::Punct(other)) => this.equals(other),
            (Self::Literal(this), Self::Literal(other)) => this.equals(other),
            _ => false,
        }
    }

    fn into_group(self) -> Result<Group, Self> {
        match self {
            Self::Group(group) => Ok(group),
            _ => Err(self),
        }
    }

    fn into_ident(self) -> Result<Ident, Self> {
        match self {
            Self::Ident(ident) => Ok(ident),
            _ => Err(self),
        }
    }

    fn into_punct(self) -> Result<Punct, Self> {
        match self {
            Self::Punct(punct) => Ok(punct),
            _ => Err(self),
        }
    }

    fn into_literal(self) -> Result<Literal, Self> {
        match self {
            Self::Literal(literal) => Ok(literal),
            _ => Err(self),
        }
    }

    fn as_group(&self) -> Option<&Group> {
        match self {
            Self::Group(group) => Some(group),
            _ => None,
        }
    }

    fn as_ident(&self) -> Option<&Ident> {
        match self {
            Self::Ident(ident) => Some(ident),
            _ => None,
        }
    }

    fn as_punct(&self) -> Option<&Punct> {
        match self {
            Self::Punct(punct) => Some(punct),
            _ => None,
        }
    }

    fn as_literal(&self) -> Option<&Literal> {
        match self {
            Self::Literal(literal) => Some(literal),
            _ => None,
        }
    }

    fn as_group_mut(&mut self) -> Option<&mut Group> {
        match self {
            Self::Group(group) => Some(group),
            _ => None,
        }
    }

    fn as_ident_mut(&mut self) -> Option<&mut Ident> {
        match self {
            Self::Ident(ident) => Some(ident),
            _ => None,
        }
    }

    fn as_punct_mut(&mut self) -> Option<&mut Punct> {
        match self {
            Self::Punct(punct) => Some(punct),
            _ => None,
        }
    }

    fn as_literal_mut(&mut self) -> Option<&mut Literal> {
        match self {
            Self::Literal(literal) => Some(literal),
            _ => None,
        }
    }
}

impl sealed::Sealed for TokenTree {}

/// Extension trait for [`Group`].
pub trait GroupExt: Sized + sealed::Sealed {
    /// Construct a group with the given span, delimiter, and token stream.
    fn new_spanned(span: Span, delimiter: Delimiter, stream: TokenStream) -> Self;

    /// Returns `true` if this group is equal to `other`.
    fn equals(&self, other: &Self) -> bool;

    /// Returns `true` if the group's delimiter matches `delimiter`.
    fn is_delimiter(&self, delimiter: Delimiter) -> bool;

    /// Returns `true` if the delimiter is [`Delimiter::Parenthesis`].
    fn is_parenthesized(&self) -> bool;

    /// Returns `true` if the delimiter is [`Delimiter::Brace`].
    fn is_braced(&self) -> bool;

    /// Returns `true` if the delimiter is [`Delimiter::Bracket`].
    fn is_bracketed(&self) -> bool;

    /// Returns `true` if the delimiter is [`Delimiter::None`].
    fn is_undelimited(&self) -> bool;
}

impl GroupExt for Group {
    fn new_spanned(span: Span, delimiter: Delimiter, stream: TokenStream) -> Self {
        let mut group = Group::new(delimiter, stream);
        group.set_span(span);
        group
    }

    fn equals(&self, other: &Self) -> bool {
        self.delimiter() == other.delimiter() && self.stream().equals(&other.stream())
    }

    fn is_delimiter(&self, delimiter: Delimiter) -> bool {
        self.delimiter() == delimiter
    }

    fn is_parenthesized(&self) -> bool {
        self.is_delimiter(Delimiter::Parenthesis)
    }

    fn is_braced(&self) -> bool {
        self.is_delimiter(Delimiter::Brace)
    }

    fn is_bracketed(&self) -> bool {
        self.is_delimiter(Delimiter::Bracket)
    }

    fn is_undelimited(&self) -> bool {
        self.is_delimiter(Delimiter::None)
    }
}

impl sealed::Sealed for Group {}

/// Extension trait for [`Ident`].
pub trait IdentExt: Sized + sealed::Sealed {
    /// Returns `true` if this ident is equal to `other`.
    ///
    /// This method is provided for completeness and simply delegates to the
    /// [`PartialEq`] implementation.
    fn equals(&self, other: &Self) -> bool;

    /// Returns `true` if the identifier is prefixed with `r#`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use met::ident;
    /// use met::IdentExt;
    ///
    /// assert!(!ident! { move }.is_raw());
    /// assert!(ident! { r#move }.is_raw());
    /// ```
    fn is_raw(&self) -> bool;

    /// If present, strips the raw identifier prefix `r#`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use met::ident;
    /// use met::IdentExt;
    ///
    /// assert_eq!(ident! { x }.unraw(), "x");
    /// assert_eq!(ident! { move }.unraw(), "move");
    /// assert_eq!(ident! { r#move }.unraw(), "move");
    /// ```
    fn unraw(&self) -> Self;
}

impl IdentExt for Ident {
    fn equals(&self, other: &Self) -> bool {
        self == other
    }

    fn is_raw(&self) -> bool {
        self.to_string().starts_with("r#")
    }

    fn unraw(&self) -> Self {
        self.to_string()
            .strip_prefix("r#")
            .map_or_else(|| self.clone(), |stripped| Self::new(stripped, self.span()))
    }
}

impl sealed::Sealed for Ident {}

/// Extension trait for [`Punct`].
pub trait PunctExt: Sized + sealed::Sealed {
    /// Construct a punct with the given span, character, and spacing.
    fn new_spanned(span: Span, character: char, spacing: Spacing) -> Self;

    /// Returns `true` if this punct is equal to `other`.
    fn equals(&self, other: &Self) -> bool;

    /// Returns `true` if the punct character is `character`.
    fn is_char(&self, character: char) -> bool;

    /// Returns `true` if the spacing matches `spacing`.
    fn is_spacing(&self, spacing: Spacing) -> bool;

    /// Returns `true` if the spacing is [`Spacing::Alone`].
    fn is_alone(&self) -> bool;

    /// Returns `true` if the spacing is [`Spacing::Joint`].
    fn is_joint(&self) -> bool;
}

impl PunctExt for Punct {
    fn new_spanned(span: Span, character: char, spacing: Spacing) -> Self {
        let mut punct = Punct::new(character, spacing);
        punct.set_span(span);
        punct
    }

    fn equals(&self, other: &Self) -> bool {
        self.as_char() == other.as_char() && self.spacing() == other.spacing()
    }

    fn is_char(&self, character: char) -> bool {
        self.as_char() == character
    }

    fn is_spacing(&self, spacing: Spacing) -> bool {
        self.spacing() == spacing
    }

    fn is_alone(&self) -> bool {
        self.is_spacing(Spacing::Alone)
    }

    fn is_joint(&self) -> bool {
        self.is_spacing(Spacing::Joint)
    }
}

impl sealed::Sealed for Punct {}

/// Extension trait for [`Literal`].
pub trait LiteralExt: Sized + sealed::Sealed {
    /// Construct a suffixed `i8` literal with the given span and integer.
    fn i8_suffixed_spanned(span: Span, integer: i8) -> Self;

    /// Construct an unsuffixed `i8` literal with the given span and integer.
    fn i8_unsuffixed_spanned(span: Span, integer: i8) -> Self;

    /// Construct a suffixed `i16` literal with the given span and integer.
    fn i16_suffixed_spanned(span: Span, integer: i16) -> Self;

    /// Construct an unsuffixed `i16` literal with the given span and integer.
    fn i16_unsuffixed_spanned(span: Span, integer: i16) -> Self;

    /// Construct a suffixed `i32` literal with the given span and integer.
    fn i32_suffixed_spanned(span: Span, integer: i32) -> Self;

    /// Construct an unsuffixed `i32` literal with the given span and integer.
    fn i32_unsuffixed_spanned(span: Span, integer: i32) -> Self;

    /// Construct a suffixed `i64` literal with the given span and integer.
    fn i64_suffixed_spanned(span: Span, integer: i64) -> Self;

    /// Construct an unsuffixed `i64` literal with the given span and integer.
    fn i64_unsuffixed_spanned(span: Span, integer: i64) -> Self;

    /// Construct a suffixed `i128` literal with the given span and integer.
    fn i128_suffixed_spanned(span: Span, integer: i128) -> Self;

    /// Construct an unsuffixed `i128` literal with the given span and integer.
    fn i128_unsuffixed_spanned(span: Span, integer: i128) -> Self;

    /// Construct a suffixed `isize` literal with the given span and integer.
    fn isize_suffixed_spanned(span: Span, integer: isize) -> Self;

    /// Construct an unsuffixed `isize` literal with the given span and integer.
    fn isize_unsuffixed_spanned(span: Span, integer: isize) -> Self;

    /// Construct a suffixed `u8` literal with the given span and integer.
    fn u8_suffixed_spanned(span: Span, integer: u8) -> Self;

    /// Construct an unsuffixed `u8` literal with the given span and integer.
    fn u8_unsuffixed_spanned(span: Span, integer: u8) -> Self;

    /// Construct a suffixed `u16` literal with the given span and integer.
    fn u16_suffixed_spanned(span: Span, integer: u16) -> Self;

    /// Construct an unsuffixed `u16` literal with the given span and integer.
    fn u16_unsuffixed_spanned(span: Span, integer: u16) -> Self;

    /// Construct a suffixed `u32` literal with the given span and integer.
    fn u32_suffixed_spanned(span: Span, integer: u32) -> Self;

    /// Construct an unsuffixed `u32` literal with the given span and integer.
    fn u32_unsuffixed_spanned(span: Span, integer: u32) -> Self;

    /// Construct a suffixed `u64` literal with the given span and integer.
    fn u64_suffixed_spanned(span: Span, integer: u64) -> Self;

    /// Construct an unsuffixed `u64` literal with the given span and integer.
    fn u64_unsuffixed_spanned(span: Span, integer: u64) -> Self;

    /// Construct a suffixed `u128` literal with the given span and integer.
    fn u128_suffixed_spanned(span: Span, integer: u128) -> Self;

    /// Construct an unsuffixed `u128` literal with the given span and integer.
    fn u128_unsuffixed_spanned(span: Span, integer: u128) -> Self;

    /// Construct a suffixed `usize` literal with the given span and integer.
    fn usize_suffixed_spanned(span: Span, integer: usize) -> Self;

    /// Construct an unsuffixed `usize` literal with the given span and integer.
    fn usize_unsuffixed_spanned(span: Span, integer: usize) -> Self;

    /// Construct a suffixed `f32` literal with the given span and float.
    fn f32_suffixed_spanned(span: Span, float: f32) -> Self;

    /// Construct an unsuffixed `f32` literal with the given span and float.
    fn f32_unsuffixed_spanned(span: Span, float: f32) -> Self;

    /// Construct a suffixed `f64` literal with the given span and float.
    fn f64_suffixed_spanned(span: Span, float: f64) -> Self;

    /// Construct an unsuffixed `f64` literal with the given span and float.
    fn f64_unsuffixed_spanned(span: Span, float: f64) -> Self;

    /// Construct a string literal with the given span and string.
    fn string_spanned(span: Span, string: &str) -> Self;

    /// Construct a byte string literal with the given span and bytes.
    fn byte_string_spanned(span: Span, bytes: &[u8]) -> Self;

    /// Construct a C-string literal with the given span and c-string.
    fn c_string_spanned(span: Span, string: &CStr) -> Self;

    /// Construct a `char` literal with the given span and character.
    fn character_spanned(span: Span, character: char) -> Self;

    /// Construct a byte character literal with the given span and byte.
    fn byte_character_spanned(span: Span, byte: u8) -> Self;

    /// Returns `true` if this literal is equal to `other`.
    fn equals(&self, other: &Self) -> bool;
}

impl LiteralExt for Literal {
    fn i8_suffixed_spanned(span: Span, integer: i8) -> Self {
        let mut literal = Self::i8_suffixed(integer);
        literal.set_span(span);
        literal
    }

    fn i8_unsuffixed_spanned(span: Span, integer: i8) -> Self {
        let mut literal = Self::i8_unsuffixed(integer);
        literal.set_span(span);
        literal
    }

    fn i16_suffixed_spanned(span: Span, integer: i16) -> Self {
        let mut literal = Self::i16_suffixed(integer);
        literal.set_span(span);
        literal
    }

    fn i16_unsuffixed_spanned(span: Span, integer: i16) -> Self {
        let mut literal = Self::i16_unsuffixed(integer);
        literal.set_span(span);
        literal
    }

    fn i32_suffixed_spanned(span: Span, integer: i32) -> Self {
        let mut literal = Self::i32_suffixed(integer);
        literal.set_span(span);
        literal
    }

    fn i32_unsuffixed_spanned(span: Span, integer: i32) -> Self {
        let mut literal = Self::i32_unsuffixed(integer);
        literal.set_span(span);
        literal
    }

    fn i64_suffixed_spanned(span: Span, integer: i64) -> Self {
        let mut literal = Self::i64_suffixed(integer);
        literal.set_span(span);
        literal
    }

    fn i64_unsuffixed_spanned(span: Span, integer: i64) -> Self {
        let mut literal = Self::i64_unsuffixed(integer);
        literal.set_span(span);
        literal
    }

    fn i128_suffixed_spanned(span: Span, integer: i128) -> Self {
        let mut literal = Self::i128_suffixed(integer);
        literal.set_span(span);
        literal
    }

    fn i128_unsuffixed_spanned(span: Span, integer: i128) -> Self {
        let mut literal = Self::i128_unsuffixed(integer);
        literal.set_span(span);
        literal
    }

    fn isize_suffixed_spanned(span: Span, integer: isize) -> Self {
        let mut literal = Self::isize_suffixed(integer);
        literal.set_span(span);
        literal
    }

    fn isize_unsuffixed_spanned(span: Span, integer: isize) -> Self {
        let mut literal = Self::isize_unsuffixed(integer);
        literal.set_span(span);
        literal
    }

    fn u8_suffixed_spanned(span: Span, integer: u8) -> Self {
        let mut literal = Self::u8_suffixed(integer);
        literal.set_span(span);
        literal
    }

    fn u8_unsuffixed_spanned(span: Span, integer: u8) -> Self {
        let mut literal = Self::u8_unsuffixed(integer);
        literal.set_span(span);
        literal
    }

    fn u16_suffixed_spanned(span: Span, integer: u16) -> Self {
        let mut literal = Self::u16_suffixed(integer);
        literal.set_span(span);
        literal
    }

    fn u16_unsuffixed_spanned(span: Span, integer: u16) -> Self {
        let mut literal = Self::u16_unsuffixed(integer);
        literal.set_span(span);
        literal
    }

    fn u32_suffixed_spanned(span: Span, integer: u32) -> Self {
        let mut literal = Self::u32_suffixed(integer);
        literal.set_span(span);
        literal
    }

    fn u32_unsuffixed_spanned(span: Span, integer: u32) -> Self {
        let mut literal = Self::u32_unsuffixed(integer);
        literal.set_span(span);
        literal
    }

    fn u64_suffixed_spanned(span: Span, integer: u64) -> Self {
        let mut literal = Self::u64_suffixed(integer);
        literal.set_span(span);
        literal
    }

    fn u64_unsuffixed_spanned(span: Span, integer: u64) -> Self {
        let mut literal = Self::u64_unsuffixed(integer);
        literal.set_span(span);
        literal
    }

    fn u128_suffixed_spanned(span: Span, integer: u128) -> Self {
        let mut literal = Self::u128_suffixed(integer);
        literal.set_span(span);
        literal
    }

    fn u128_unsuffixed_spanned(span: Span, integer: u128) -> Self {
        let mut literal = Self::u128_unsuffixed(integer);
        literal.set_span(span);
        literal
    }

    fn usize_suffixed_spanned(span: Span, integer: usize) -> Self {
        let mut literal = Self::usize_suffixed(integer);
        literal.set_span(span);
        literal
    }

    fn usize_unsuffixed_spanned(span: Span, integer: usize) -> Self {
        let mut literal = Self::usize_unsuffixed(integer);
        literal.set_span(span);
        literal
    }

    fn f32_suffixed_spanned(span: Span, float: f32) -> Self {
        let mut literal = Self::f32_suffixed(float);
        literal.set_span(span);
        literal
    }

    fn f32_unsuffixed_spanned(span: Span, float: f32) -> Self {
        let mut literal = Self::f32_unsuffixed(float);
        literal.set_span(span);
        literal
    }

    fn f64_suffixed_spanned(span: Span, float: f64) -> Self {
        let mut literal = Self::f64_suffixed(float);
        literal.set_span(span);
        literal
    }

    fn f64_unsuffixed_spanned(span: Span, float: f64) -> Self {
        let mut literal = Self::f64_unsuffixed(float);
        literal.set_span(span);
        literal
    }

    fn string_spanned(span: Span, string: &str) -> Self {
        let mut literal = Self::string(string);
        literal.set_span(span);
        literal
    }

    fn byte_string_spanned(span: Span, bytes: &[u8]) -> Self {
        let mut literal = Self::byte_string(bytes);
        literal.set_span(span);
        literal
    }

    fn c_string_spanned(span: Span, string: &CStr) -> Self {
        let mut literal = Self::c_string(string);
        literal.set_span(span);
        literal
    }

    fn character_spanned(span: Span, character: char) -> Self {
        let mut literal = Self::character(character);
        literal.set_span(span);
        literal
    }

    fn byte_character_spanned(span: Span, byte: u8) -> Self {
        let mut literal = Self::byte_character(byte);
        literal.set_span(span);
        literal
    }

    fn equals(&self, other: &Self) -> bool {
        self.to_string() == other.to_string()
    }
}

impl sealed::Sealed for Literal {}