Struct litrs::IntegerLit

source ·
#[non_exhaustive]
pub struct IntegerLit<B: Buffer> { /* private fields */ }
Expand description

An integer literal, e.g. 27, 0x7F, 0b101010u8 or 5_000_000i64.

An integer literal consists of an optional base prefix (0b, 0o, 0x), the main part (digits and underscores), and an optional type suffix (e.g. u64 or i8). See the reference for more information.

Note that integer literals are always positive: the grammar does not contain the minus sign at all. The minus sign is just the unary negate operator, not part of the literal. Which is interesting for cases like - 128i8: here, the literal itself would overflow the specified type (i8 cannot represent 128). That’s why in rustc, the literal overflow check is performed as a lint after parsing, not during the lexing stage. Similarly, IntegerLit::parse does not perform an overflow check.

Implementations§

source§

impl<B: Buffer> IntegerLit<B>

source

pub fn parse(input: B) -> Result<Self, ParseError>

Parses the input as an integer literal. Returns an error if the input is invalid or represents a different kind of literal.

source

pub fn value<N: FromIntegerLiteral>(&self) -> Option<N>

Performs the actual string to int conversion to obtain the integer value. The optional type suffix of the literal is ignored by this method. This means N does not need to match the type suffix!

Returns None if the literal overflows N.

Hint: u128 can represent all possible values integer literal values, as there are no negative literals (see type docs). Thus you can, for example, safely use lit.value::<u128>().to_string() to get a decimal string. (Technically, Rust integer literals can represent arbitrarily large numbers, but those would be rejected at a later stage by the Rust compiler).

source

pub fn base(&self) -> IntegerBase

The base of this integer literal.

source

pub fn raw_main_part(&self) -> &str

The main part containing the digits and potentially _. Do not try to parse this directly as that would ignore the base!

source

pub fn suffix(&self) -> &str

The optional suffix. Returns "" if the suffix is empty/does not exist.

If you want the type, try IntegerType::from_suffix(lit.suffix()).

source

pub fn raw_input(&self) -> &str

Returns the raw input that was passed to parse.

source

pub fn into_raw_input(self) -> B

Returns the raw input that was passed to parse, potentially owned.

source§

impl IntegerLit<&str>

source

pub fn to_owned(&self) -> IntegerLit<String>

Makes a copy of the underlying buffer and returns the owned version of Self.

Trait Implementations§

source§

impl<B: Clone + Buffer> Clone for IntegerLit<B>

source§

fn clone(&self) -> IntegerLit<B>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<B: Debug + Buffer> Debug for IntegerLit<B>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<B: Buffer> Display for IntegerLit<B>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<B: Buffer> From<IntegerLit<B>> for Literal

source§

fn from(l: IntegerLit<B>) -> Self

Converts to this type from the input type.
source§

impl<B: Buffer> From<IntegerLit<B>> for Literal<B>

source§

fn from(src: IntegerLit<B>) -> Self

Converts to this type from the input type.
source§

impl<B: Buffer> From<IntegerLit<B>> for Literal

source§

fn from(l: IntegerLit<B>) -> Self

Converts to this type from the input type.
source§

impl<B: PartialEq + Buffer> PartialEq for IntegerLit<B>

source§

fn eq(&self, other: &IntegerLit<B>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl TryFrom<&Literal> for IntegerLit<String>

§

type Error = InvalidToken

The type returned in the event of a conversion error.
source§

fn try_from(src: &Literal) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<&Literal> for IntegerLit<String>

§

type Error = InvalidToken

The type returned in the event of a conversion error.
source§

fn try_from(src: &Literal) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<&TokenTree> for IntegerLit<String>

§

type Error = InvalidToken

The type returned in the event of a conversion error.
source§

fn try_from(tt: &TokenTree) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<&TokenTree> for IntegerLit<String>

§

type Error = InvalidToken

The type returned in the event of a conversion error.
source§

fn try_from(tt: &TokenTree) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<Literal> for IntegerLit<String>

§

type Error = InvalidToken

The type returned in the event of a conversion error.
source§

fn try_from(src: Literal) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<Literal> for IntegerLit<String>

§

type Error = InvalidToken

The type returned in the event of a conversion error.
source§

fn try_from(src: Literal) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<TokenTree> for IntegerLit<String>

§

type Error = InvalidToken

The type returned in the event of a conversion error.
source§

fn try_from(tt: TokenTree) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<TokenTree> for IntegerLit<String>

§

type Error = InvalidToken

The type returned in the event of a conversion error.
source§

fn try_from(tt: TokenTree) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<B: Copy + Buffer> Copy for IntegerLit<B>

source§

impl<B: Eq + Buffer> Eq for IntegerLit<B>

source§

impl<B: Buffer> StructuralEq for IntegerLit<B>

source§

impl<B: Buffer> StructuralPartialEq for IntegerLit<B>

Auto Trait Implementations§

§

impl<B> RefUnwindSafe for IntegerLit<B>
where B: RefUnwindSafe,

§

impl<B> Send for IntegerLit<B>
where B: Send,

§

impl<B> Sync for IntegerLit<B>
where B: Sync,

§

impl<B> Unpin for IntegerLit<B>
where B: Unpin,

§

impl<B> UnwindSafe for IntegerLit<B>
where B: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.