Enum litrs::Literal

source ·
pub enum Literal<B: Buffer> {
    Bool(BoolLit),
    Integer(IntegerLit<B>),
    Float(FloatLit<B>),
    Char(CharLit<B>),
    String(StringLit<B>),
    Byte(ByteLit<B>),
    ByteString(ByteStringLit<B>),
}
Expand description

A literal. This is the main type of this library.

This type is generic over the underlying buffer B, which can be &str or String.

To create this type, you have to either call Literal::parse with an input string or use the From<_> impls of this type. The impls are only available of the corresponding crate features are enabled (they are enabled by default).

Variants§

§

Bool(BoolLit)

§

Integer(IntegerLit<B>)

§

Float(FloatLit<B>)

§

Char(CharLit<B>)

§

String(StringLit<B>)

§

Byte(ByteLit<B>)

§

ByteString(ByteStringLit<B>)

Implementations§

source§

impl<B: Buffer> Literal<B>

source

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

Parses the given input as a Rust literal.

source

pub fn suffix(&self) -> &str

Returns the suffix of this literal or "" if it doesn’t have one.

Rust token grammar actually allows suffixes for all kinds of tokens. Most Rust programmer only know the type suffixes for integer and floats, e.g. 0u32. And in normal Rust code, everything else causes an error. But it is possible to pass literals with arbitrary suffixes to proc macros, for example:

some_macro!(3.14f33  16px  '🦊'good_boy  "toph"beifong);

Boolean literals, not actually being literals, but idents, cannot have suffixes and this method always returns "" for those.

There are some edge cases to be aware of:

  • Integer suffixes must not start with e or E as that conflicts with the exponent grammar for floats. 0e1 is a float; 0eel is also parsed as a float and results in an error.
  • Hexadecimal integers eagerly parse digits, so 0x5abcdefgh has a suffix von gh.
  • Suffixes can contain and start with _, but for integer and number literals, _ is eagerly parsed as part of the number, so 1_x has the suffix x.
  • The input 55f32 is regarded as integer literal with suffix f32.
Example
use litrs::Literal;

assert_eq!(Literal::parse(r##"3.14f33"##).unwrap().suffix(), "f33");
assert_eq!(Literal::parse(r##"123hackerman"##).unwrap().suffix(), "hackerman");
assert_eq!(Literal::parse(r##"0x0fuck"##).unwrap().suffix(), "uck");
assert_eq!(Literal::parse(r##"'🦊'good_boy"##).unwrap().suffix(), "good_boy");
assert_eq!(Literal::parse(r##""toph"beifong"##).unwrap().suffix(), "beifong");
source§

impl Literal<&str>

source

pub fn into_owned(self) -> Literal<String>

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

Trait Implementations§

source§

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

source§

fn clone(&self) -> Literal<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 Literal<B>

source§

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

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

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

source§

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

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

impl From<&Literal> for Literal<String>

source§

fn from(src: &Literal) -> Self

Converts to this type from the input type.
source§

impl From<&Literal> for Literal<String>

source§

fn from(src: &Literal) -> Self

Converts to this type from the input type.
source§

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

source§

fn from(src: BoolLit) -> Self

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

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

source§

fn from(src: FloatLit<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 From<Literal> for Literal<String>

source§

fn from(src: Literal) -> Self

Converts to this type from the input type.
source§

impl From<Literal> for Literal<String>

source§

fn from(src: Literal) -> Self

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

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

source§

fn eq(&self, other: &Literal<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<&TokenTree> for Literal<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 Literal<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 Literal<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 Literal<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: Eq + Buffer> Eq for Literal<B>

source§

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

source§

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<B> UnwindSafe for Literal<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.