Skip to main content

FromLit

Trait FromLit 

Source
pub trait FromLit: Sized {
    // Required method
    fn from_lit(lit: Literal) -> Result<Self, TokenStream>;

    // Provided methods
    fn from_ident(ident: Ident) -> Result<Self, TokenStream> { ... }
    fn from_negative_lit(lit: Literal) -> Result<Self, TokenStream> { ... }
}
Expand description

Converts literal tokens into typed values.

This trait is the core extension point for litext. Implementing it enables litext!(input as MyType) to work for custom types.

§Implementations

The trait is automatically implemented for all built-in literal types:

§Method Overview

Override from_ident for types represented as identifiers (like bool). Override from_negative_lit for signed numeric types. The default implementations return errors, which is correct for non-numeric types.

§Example

use litext::literal::FromLit;
use proc_macro2::{Literal, TokenStream};

pub struct UpperString(String);

impl FromLit for UpperString {
    fn from_lit(lit: Literal) -> Result<Self, TokenStream> {
        let s = String::from_lit(lit)?;
        Ok(UpperString(s.to_uppercase()))
    }
}

// Then in your macro:
// let val: UpperString = litext!(input as UpperString);

§See Also

Required Methods§

Source

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

Attempts to extract a value from a Literal token.

On success, returns Ok containing the extracted value.

§Errors

On failure, returns Err containing a TokenStream that triggers a compile error when returned from a proc-macro.

Provided Methods§

Source

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

Attempts to extract a value from an Ident token.

Most types will never receive an ident, the default implementation returns an error. Override this for types like bool where the literal representation is an identifier (true, false).

§Errors

Errors to this function is in the form of a compile_error!() token stream, the cause completely up to the implementer.

Source

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

Attempts to extract a value from a negative Literal token.

A ‘negative Literal’ token in this case is the negative number, a dash punctuation followed by a number. Most types won’t accept or receive a negative literal. Override this for signed integer / float types where it accepts negatives as a value.

§Errors

Errors to this function is in the form of a compile_error!() token stream, the cause completely up to the implementer.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl FromLit for bool

Source§

impl FromLit for char

Source§

impl FromLit for f32

Source§

impl FromLit for f64

Source§

impl FromLit for i8

Source§

impl FromLit for i16

Source§

impl FromLit for i32

Source§

impl FromLit for i64

Source§

impl FromLit for i128

Source§

impl FromLit for isize

Source§

impl FromLit for u8

Source§

impl FromLit for u16

Source§

impl FromLit for u32

Source§

impl FromLit for u64

Source§

impl FromLit for u128

Source§

impl FromLit for usize

Source§

impl FromLit for CString

Source§

impl FromLit for String

Source§

impl FromLit for Vec<u8>

Implementors§

Source§

impl FromLit for LitBool

Source§

impl FromLit for LitByte

Source§

impl FromLit for LitByteStr

Source§

impl FromLit for LitCStr

Source§

impl FromLit for LitChar

Source§

impl FromLit for LitStr

Source§

impl<T> FromLit for LitFloat<T>
where T: FloatSeal + Neg<Output = T> + FromStr, T::Err: Display,

Source§

impl<T> FromLit for LitInt<T>
where T: TryFrom<u128> + TryFrom<i128> + IntegerSeal, <T as TryFrom<u128>>::Error: Display, <T as TryFrom<i128>>::Error: Display,