Enum parsel::ast::Lit

source ·
pub enum Lit {
    Bool(LitBool),
    Byte(LitByte),
    Int(LitInt),
    Uint(LitUint),
    Float(LitFloat),
    Char(LitChar),
    Str(LitStr),
    ByteStr(LitByteStr),
}
Expand description

Represents any literal value.

The following is a lazy doc-test for ensuring that the Span of literals is preserved when converted between parsel::ast::Lit and syn::Lit.

// This deliberately uses `parse_str()` instead of `parse_quote!{}`
// because the former results in meaningful `Span` values, while
// the latter would cause every token to be spanned to the call site.
let source = r#"
true
   false  6.283
  100000023456         b'\xa3'
      "foo bar baz" - 74819253
   -987766554433221100
    b"This is a byte string! A literal one."
"#;
let parsel: Many<Lit> = parsel::parse_str(source).unwrap();
let syn: Many<syn::Lit> = parsel::parse_str(source).unwrap();

let parsel_strings: Vec<_> = parsel
    .iter()
    .map(Lit::to_string)
    .collect();
let syn_strings: Vec<_> = syn
    .iter()
    .map(|lit| -> String {
        // if we simply used `TokenStream::to_string()` here, then multi-token
        // negative literals would fail the test since their representation
        // sometimes includes a space between the sign and the digits.
        lit.to_token_stream().into_iter().map(|tt| tt.to_string()).collect()
    })
    .collect();

assert_eq!(parsel_strings, syn_strings);

let parsel_spans: Vec<_> = parsel
    .iter()
    .map(Lit::span)
    .map(|span| (span.start(), span.end()))
    .collect();
let syn_spans: Vec<_> = syn
    .iter()
    .map(syn::Lit::span)
    .map(|span| (span.start(), span.end()))
    .collect();

assert_eq!(parsel_spans, syn_spans);

let parsel_converted_spans: Vec<_> = parsel
    .into_iter()
    .map(|lit| syn::Lit::from(lit).span())
    .map(|span| (span.start(), span.end()))
    .collect();
let syn_converted_spans: Vec<_> = syn
    .into_iter()
    .map(|lit| Lit::try_from(lit).unwrap().span())
    .map(|span| (span.start(), span.end()))
    .collect();

assert_eq!(parsel_converted_spans, syn_converted_spans);

Variants§

§

Bool(LitBool)

§

Byte(LitByte)

§

Int(LitInt)

§

Uint(LitUint)

§

Float(LitFloat)

§

Char(LitChar)

§

Str(LitStr)

§

ByteStr(LitByteStr)

Implementations§

source§

impl Lit

source

pub const fn span(&self) -> Span

source

pub fn set_span(&mut self, new_span: Span)

Trait Implementations§

source§

impl Clone for Lit

source§

fn clone(&self) -> Lit

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 Debug for Lit

source§

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

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

impl Display for Lit

source§

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

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

impl From<&[u8]> for Lit

source§

fn from(value: &[u8]) -> Self

Converts to this type from the input type.
source§

impl From<&str> for Lit

source§

fn from(value: &str) -> Self

Converts to this type from the input type.
source§

impl From<Box<[u8]>> for Lit

source§

fn from(value: Box<[u8]>) -> Self

Converts to this type from the input type.
source§

impl From<Box<str>> for Lit

source§

fn from(value: Box<str>) -> Self

Converts to this type from the input type.
source§

impl From<Cow<'_, [u8]>> for Lit

source§

fn from(value: Cow<'_, [u8]>) -> Self

Converts to this type from the input type.
source§

impl From<Cow<'_, str>> for Lit

source§

fn from(value: Cow<'_, str>) -> Self

Converts to this type from the input type.
source§

impl From<Lit> for Lit

source§

fn from(lit: Lit) -> Self

Converts to this type from the input type.
source§

impl From<LitBool> for Lit

source§

fn from(lit: LitBool) -> Self

Converts to this type from the input type.
source§

impl From<LitByte> for Lit

source§

fn from(lit: LitByte) -> Self

Converts to this type from the input type.
source§

impl From<LitByteStr> for Lit

source§

fn from(lit: LitByteStr) -> Self

Converts to this type from the input type.
source§

impl From<LitChar> for Lit

source§

fn from(lit: LitChar) -> Self

Converts to this type from the input type.
source§

impl From<LitFloat> for Lit

source§

fn from(lit: LitFloat) -> Self

Converts to this type from the input type.
source§

impl From<LitInt> for Lit

source§

fn from(lit: LitInt) -> Self

Converts to this type from the input type.
source§

impl From<LitStr> for Lit

source§

fn from(lit: LitStr) -> Self

Converts to this type from the input type.
source§

impl From<LitUint> for Lit

source§

fn from(lit: LitUint) -> Self

Converts to this type from the input type.
source§

impl From<NotNan<f64>> for Lit

source§

fn from(value: NotNan<f64>) -> Self

Converts to this type from the input type.
source§

impl From<String> for Lit

source§

fn from(value: String) -> Self

Converts to this type from the input type.
source§

impl From<Vec<u8>> for Lit

source§

fn from(value: Vec<u8>) -> Self

Converts to this type from the input type.
source§

impl From<bool> for Lit

source§

fn from(value: bool) -> Self

Converts to this type from the input type.
source§

impl From<char> for Lit

source§

fn from(value: char) -> Self

Converts to this type from the input type.
source§

impl From<i64> for Lit

source§

fn from(value: i64) -> Self

Converts to this type from the input type.
source§

impl From<u64> for Lit

source§

fn from(value: u64) -> Self

Converts to this type from the input type.
source§

impl From<u8> for Lit

source§

fn from(value: u8) -> Self

Converts to this type from the input type.
source§

impl FromStr for Lit

§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(string: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
source§

impl Hash for Lit

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Parse for Lit

source§

fn parse(input: ParseStream<'_>) -> Result<Self>

source§

impl PartialEq for Lit

source§

fn eq(&self, other: &Lit) -> 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 ToTokens for Lit

source§

fn to_tokens(&self, tokens: &mut TokenStream)

Write self to the given TokenStream. Read more
source§

fn to_token_stream(&self) -> TokenStream

Convert self directly into a TokenStream object. Read more
source§

fn into_token_stream(self) -> TokenStream
where Self: Sized,

Convert self directly into a TokenStream object. Read more
source§

impl TryFrom<Lit> for Lit

§

type Error = Error

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

fn try_from(lit: Lit) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<f64> for Lit

§

type Error = FloatIsNan

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

fn try_from(value: f64) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl Eq for Lit

source§

impl StructuralPartialEq for Lit

Auto Trait Implementations§

§

impl Freeze for Lit

§

impl RefUnwindSafe for Lit

§

impl !Send for Lit

§

impl !Sync for Lit

§

impl Unpin for Lit

§

impl UnwindSafe for Lit

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> Spanned for T
where T: Spanned + ?Sized,

source§

fn span(&self) -> Span

Returns a Span covering the complete contents of this syntax tree node, or Span::call_site() if this node is empty.
source§

impl<T> SpannedExt for T
where T: Spanned + ?Sized,

source§

fn span(&self) -> Span

source§

fn format_span(&self) -> SpanDisplay

source§

fn source_substring<'s>(&self, source: &'s str) -> &'s str

source§

fn byte_range(&self, source: &str) -> Range<usize>

TODO(H2CO3): a faster, less naive implementation would be great. We should use the byte offset of start to compute that of end, sparing the double scan of the source up until the start location. Read more
source§

fn char_range(&self, source: &str) -> Range<usize>

TODO(H2CO3): a faster, less naive implementation would be great. We should use the char offset of start to compute that of end, sparing the double scan of the source up until the start location. Read more
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.