Struct proc_macro2::Literal

source ·
pub struct Literal { /* private fields */ }
Expand description

A literal string ("hello"), byte string (b"hello"), character ('a'), byte character (b'a'), an integer or floating point number with or without a suffix (1, 1u8, 2.3, 2.3f32).

Boolean literals like true and false do not belong here, they are Idents.

Implementations§

Creates a new suffixed integer literal with the specified value.

This function will create an integer like 1u32 where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new suffixed integer literal with the specified value.

This function will create an integer like 1u32 where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new suffixed integer literal with the specified value.

This function will create an integer like 1u32 where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new suffixed integer literal with the specified value.

This function will create an integer like 1u32 where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new suffixed integer literal with the specified value.

This function will create an integer like 1u32 where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new suffixed integer literal with the specified value.

This function will create an integer like 1u32 where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new suffixed integer literal with the specified value.

This function will create an integer like 1u32 where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new suffixed integer literal with the specified value.

This function will create an integer like 1u32 where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new suffixed integer literal with the specified value.

This function will create an integer like 1u32 where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new suffixed integer literal with the specified value.

This function will create an integer like 1u32 where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new suffixed integer literal with the specified value.

This function will create an integer like 1u32 where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new suffixed integer literal with the specified value.

This function will create an integer like 1u32 where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like 1 where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like Literal::i8_unsuffixed(1) are equivalent to Literal::u32_unsuffixed(1). Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like 1 where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like Literal::i8_unsuffixed(1) are equivalent to Literal::u32_unsuffixed(1). Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like 1 where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like Literal::i8_unsuffixed(1) are equivalent to Literal::u32_unsuffixed(1). Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like 1 where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like Literal::i8_unsuffixed(1) are equivalent to Literal::u32_unsuffixed(1). Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like 1 where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like Literal::i8_unsuffixed(1) are equivalent to Literal::u32_unsuffixed(1). Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like 1 where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like Literal::i8_unsuffixed(1) are equivalent to Literal::u32_unsuffixed(1). Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like 1 where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like Literal::i8_unsuffixed(1) are equivalent to Literal::u32_unsuffixed(1). Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like 1 where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like Literal::i8_unsuffixed(1) are equivalent to Literal::u32_unsuffixed(1). Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like 1 where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like Literal::i8_unsuffixed(1) are equivalent to Literal::u32_unsuffixed(1). Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like 1 where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like Literal::i8_unsuffixed(1) are equivalent to Literal::u32_unsuffixed(1). Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like 1 where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like Literal::i8_unsuffixed(1) are equivalent to Literal::u32_unsuffixed(1). Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like 1 where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like Literal::i8_unsuffixed(1) are equivalent to Literal::u32_unsuffixed(1). Literals created from negative numbers may not survive roundtrips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Literals created through this method have the Span::call_site() span by default, which can be configured with the set_span method below.

Creates a new unsuffixed floating-point literal.

This constructor is similar to those like Literal::i8_unsuffixed where the float’s value is emitted directly into the token but no suffix is used, so it may be inferred to be a f64 later in the compiler. Literals created from negative numbers may not survive round-trips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Panics

This function requires that the specified float is finite, for example if it is infinity or NaN this function will panic.

Creates a new suffixed floating-point literal.

This constructor will create a literal like 1.0f64 where the value specified is the preceding part of the token and f64 is the suffix of the token. This token will always be inferred to be an f64 in the compiler. Literals created from negative numbers may not survive round-trips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Panics

This function requires that the specified float is finite, for example if it is infinity or NaN this function will panic.

Creates a new unsuffixed floating-point literal.

This constructor is similar to those like Literal::i8_unsuffixed where the float’s value is emitted directly into the token but no suffix is used, so it may be inferred to be a f64 later in the compiler. Literals created from negative numbers may not survive round-trips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Panics

This function requires that the specified float is finite, for example if it is infinity or NaN this function will panic.

Creates a new suffixed floating-point literal.

This constructor will create a literal like 1.0f32 where the value specified is the preceding part of the token and f32 is the suffix of the token. This token will always be inferred to be an f32 in the compiler. Literals created from negative numbers may not survive round-trips through TokenStream or strings and may be broken into two tokens (- and positive literal).

Panics

This function requires that the specified float is finite, for example if it is infinity or NaN this function will panic.

String literal.

Examples found in repository?
src/parse.rs (line 823)
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
fn doc_comment<'a>(input: Cursor<'a>, trees: &mut TokenStreamBuilder) -> PResult<'a, ()> {
    #[cfg(span_locations)]
    let lo = input.off;
    let (rest, (comment, inner)) = doc_comment_contents(input)?;
    let span = crate::Span::_new_stable(Span {
        #[cfg(span_locations)]
        lo,
        #[cfg(span_locations)]
        hi: rest.off,
    });

    let mut scan_for_bare_cr = comment;
    while let Some(cr) = scan_for_bare_cr.find('\r') {
        let rest = &scan_for_bare_cr[cr + 1..];
        if !rest.starts_with('\n') {
            return Err(Reject);
        }
        scan_for_bare_cr = rest;
    }

    let mut pound = Punct::new('#', Spacing::Alone);
    pound.set_span(span);
    trees.push_token_from_parser(TokenTree::Punct(pound));

    if inner {
        let mut bang = Punct::new('!', Spacing::Alone);
        bang.set_span(span);
        trees.push_token_from_parser(TokenTree::Punct(bang));
    }

    let doc_ident = crate::Ident::new("doc", span);
    let mut equal = Punct::new('=', Spacing::Alone);
    equal.set_span(span);
    let mut literal = crate::Literal::string(comment);
    literal.set_span(span);
    let mut bracketed = TokenStreamBuilder::with_capacity(3);
    bracketed.push_token_from_parser(TokenTree::Ident(doc_ident));
    bracketed.push_token_from_parser(TokenTree::Punct(equal));
    bracketed.push_token_from_parser(TokenTree::Literal(literal));
    let group = Group::new(Delimiter::Bracket, bracketed.build());
    let mut group = crate::Group::_new_stable(group);
    group.set_span(span);
    trees.push_token_from_parser(TokenTree::Group(group));

    Ok((rest, ()))
}

Character literal.

Byte string literal.

Returns the span encompassing this literal.

Examples found in repository?
src/lib.rs (line 584)
579
580
581
582
583
584
585
586
    pub fn span(&self) -> Span {
        match self {
            TokenTree::Group(t) => t.span(),
            TokenTree::Ident(t) => t.span(),
            TokenTree::Punct(t) => t.span(),
            TokenTree::Literal(t) => t.span(),
        }
    }

Configures the span associated for this literal.

Examples found in repository?
src/lib.rs (line 598)
593
594
595
596
597
598
599
600
    pub fn set_span(&mut self, span: Span) {
        match self {
            TokenTree::Group(t) => t.set_span(span),
            TokenTree::Ident(t) => t.set_span(span),
            TokenTree::Punct(t) => t.set_span(span),
            TokenTree::Literal(t) => t.set_span(span),
        }
    }
More examples
Hide additional examples
src/parse.rs (line 824)
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
fn doc_comment<'a>(input: Cursor<'a>, trees: &mut TokenStreamBuilder) -> PResult<'a, ()> {
    #[cfg(span_locations)]
    let lo = input.off;
    let (rest, (comment, inner)) = doc_comment_contents(input)?;
    let span = crate::Span::_new_stable(Span {
        #[cfg(span_locations)]
        lo,
        #[cfg(span_locations)]
        hi: rest.off,
    });

    let mut scan_for_bare_cr = comment;
    while let Some(cr) = scan_for_bare_cr.find('\r') {
        let rest = &scan_for_bare_cr[cr + 1..];
        if !rest.starts_with('\n') {
            return Err(Reject);
        }
        scan_for_bare_cr = rest;
    }

    let mut pound = Punct::new('#', Spacing::Alone);
    pound.set_span(span);
    trees.push_token_from_parser(TokenTree::Punct(pound));

    if inner {
        let mut bang = Punct::new('!', Spacing::Alone);
        bang.set_span(span);
        trees.push_token_from_parser(TokenTree::Punct(bang));
    }

    let doc_ident = crate::Ident::new("doc", span);
    let mut equal = Punct::new('=', Spacing::Alone);
    equal.set_span(span);
    let mut literal = crate::Literal::string(comment);
    literal.set_span(span);
    let mut bracketed = TokenStreamBuilder::with_capacity(3);
    bracketed.push_token_from_parser(TokenTree::Ident(doc_ident));
    bracketed.push_token_from_parser(TokenTree::Punct(equal));
    bracketed.push_token_from_parser(TokenTree::Literal(literal));
    let group = Group::new(Delimiter::Bracket, bracketed.build());
    let mut group = crate::Group::_new_stable(group);
    group.set_span(span);
    trees.push_token_from_parser(TokenTree::Group(group));

    Ok((rest, ()))
}

Returns a Span that is a subset of self.span() containing only the source bytes in range range. Returns None if the would-be trimmed span is outside the bounds of self.

Warning: the underlying proc_macro::Literal::subspan method is nightly-only. When called from within a procedural macro not using a nightly compiler, this method will always return None.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.