Skip to main content

LitBool

Struct LitBool 

Source
pub struct LitBool {
    pub value: bool,
    pub span: Span,
}
Expand description

A parsed boolean literal bundled with its source location.

true and false are identifier tokens in Rust’s token stream, not literal tokens. The FromLit implementation overrides FromLit::from_ident to handle them. FromLit::from_lit always returns an error.

Use LitBool when you need the span. Use bool when you only need the value.

§Example

use litext::{litext, TokenStream};
use litext::literal::LitBool;

pub fn my_macro(input: TokenStream) -> TokenStream {
    let lit: LitBool = litext!(input as LitBool);
    if !lit.value() {
        return comperr::error(lit.span(), "feature must be enabled");
    }
    quote::quote! { /* use lit.value() */ }
}

Fields§

§value: bool

The parsed boolean value.

§span: Span

The source location of the literal.

Implementations§

Source§

impl LitBool

Source

pub const fn new(value: bool, span: Span) -> Self

Creates a new LitBool from a value and a span.

Source

pub const fn value(&self) -> bool

Returns the parsed boolean value.

Source

pub const fn span(&self) -> Span

Returns the source span of the literal.

Trait Implementations§

Source§

impl FromLit for LitBool

Source§

fn from_lit(lit: Literal) -> Result<Self, TokenStream>

Attempts to extract a value from a Literal token. Read more
Source§

fn from_ident(ident: Ident) -> Result<Self, TokenStream>

Attempts to extract a value from an Ident token. Read more
Source§

fn from_negative_lit(lit: Literal) -> Result<Self, TokenStream>

Attempts to extract a value from a negative Literal token. Read more
Source§

impl ToTokens for LitBool

Source§

fn to_token_stream(&self) -> TokenStream

true and false are Ident tokens, not Literal tokens.

Auto Trait Implementations§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.