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
-99887766554433221100
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
Trait Implementations
sourceimpl From<LitByteStr> for Lit
impl From<LitByteStr> for Lit
sourcefn from(lit: LitByteStr) -> Self
fn from(lit: LitByteStr) -> Self
Converts to this type from the input type.
sourceimpl ToTokens for Lit
impl ToTokens for Lit
sourcefn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
Write self
to the given TokenStream
. Read more
sourcefn to_token_stream(&self) -> TokenStream
fn to_token_stream(&self) -> TokenStream
Convert self
directly into a TokenStream
object. Read more
sourcefn into_token_stream(self) -> TokenStream
fn into_token_stream(self) -> TokenStream
Convert self
directly into a TokenStream
object. Read more
impl Eq for Lit
impl StructuralEq for Lit
impl StructuralPartialEq for Lit
Auto Trait Implementations
impl RefUnwindSafe for Lit
impl !Send for Lit
impl !Sync for Lit
impl Unpin for Lit
impl UnwindSafe for Lit
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Spanned for T where
T: Spanned + ?Sized,
impl<T> Spanned for T where
T: Spanned + ?Sized,
sourcefn span(&self) -> Span
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. Read more