pub enum NumberArg {
Literal(LitInt),
ConstExpr {
const_token: Const,
kind: NumberKind,
block: Block,
},
Constant {
kind: NumberKind,
dbl_colon: PathSep,
ident: MinOrMax,
},
}Expand description
Represents a numerical argument obtained from parsing proc macro input tokens.
NumberArg can represent three kinds of numerical data:
- A literal value directly specified in the input.
- A primitive number constant, which is a predefined constant value.
- An expression that is evaluated at compile time using the Rhai scripting engine.
This enum is designed to work within the context of procedural macros, where it helps in interpreting and manipulating numerical inputs. It provides flexibility in handling both simple numerical values and complex expressions that need to be evaluated.
The NumberArg can be converted into a NumberValue type through the into_value method.
This conversion allows for direct operations on the underlying numerical value, supporting
various primitive number types (e.g., u8, u16, i32, usize, etc.).
Example usage:
let num_arg: NumberArg = parse_macro_input!(input as NumberArg);
let num_value: NumberValue = num_arg.into_value(NumberKind::U32);
// Now `num_value` can be used for further numerical operations.Note: The into_value method panics if the conversion fails, so it should be used
within a context where panicking is acceptable or handled appropriately.
Variants§
Implementations§
Source§impl NumberArg
impl NumberArg
pub const LIMITS_INIT: (Option<Self>, Option<Self>)
Sourcepub fn new_min_constant(kind: NumberKind) -> Self
pub fn new_min_constant(kind: NumberKind) -> Self
Create a new NumberArg instance for a NumberKind’s minimum value.
Sourcepub fn new_max_constant(kind: NumberKind) -> Self
pub fn new_max_constant(kind: NumberKind) -> Self
Create a new NumberArg instance for a NumberKind’s maximum value.
Sourcepub fn from_expr(expr: &Expr) -> Self
pub fn from_expr(expr: &Expr) -> Self
Helper method to create a NumberArg instance from a syn::Expr. Mostly useful for testing.
Sourcepub fn from_lit(lit: &LitInt) -> Self
pub fn from_lit(lit: &LitInt) -> Self
Helper method to create a NumberArg instance from a syn::LitInt. Mostly useful for testing.
Sourcepub fn from_range_expr(kind: NumberKind, expr: &ExprRange) -> (Self, Self)
pub fn from_range_expr(kind: NumberKind, expr: &ExprRange) -> (Self, Self)
Helper method to create a pair of NumberArg instances from a syn::ExprRange. Mostly useful for testing.
Sourcepub fn min(&self, other: &Self, kind: NumberKind) -> Self
pub fn min(&self, other: &Self, kind: NumberKind) -> Self
Create a new NumberArg instance from the lesser of two inputs.
Sourcepub fn max(&self, other: &Self, kind: NumberKind) -> Self
pub fn max(&self, other: &Self, kind: NumberKind) -> Self
Create a new NumberArg instance from the greater of two inputs.
Sourcepub fn into_value(&self, kind: NumberKind) -> NumberValue
pub fn into_value(&self, kind: NumberKind) -> NumberValue
Convert the NumberArg into a NumberValue instance.
Sourcepub fn into_literal_as_tokens(&self, kind: NumberKind) -> TokenStream
pub fn into_literal_as_tokens(&self, kind: NumberKind) -> TokenStream
Output the value as a bare literal number in a token stream.
Trait Implementations§
Source§impl ToTokens for NumberArg
impl ToTokens for NumberArg
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
Source§fn to_token_stream(&self) -> TokenStream
fn to_token_stream(&self) -> TokenStream
Source§fn into_token_stream(self) -> TokenStreamwhere
Self: Sized,
fn into_token_stream(self) -> TokenStreamwhere
Self: Sized,
Auto Trait Implementations§
impl Freeze for NumberArg
impl RefUnwindSafe for NumberArg
impl !Send for NumberArg
impl !Sync for NumberArg
impl Unpin for NumberArg
impl UnwindSafe for NumberArg
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Spanned for Twhere
T: Spanned + ?Sized,
impl<T> Spanned for Twhere
T: Spanned + ?Sized,
Source§fn span(&self) -> Span
fn span(&self) -> Span
Span covering the complete contents of this syntax tree
node, or Span::call_site() if this node is empty.Source§impl<T> Variant for T
impl<T> Variant for T
Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Variant trait object to &mut dyn Any.Source§fn as_boxed_any(self: Box<T>) -> Box<dyn Any>
fn as_boxed_any(self: Box<T>) -> Box<dyn Any>
Variant trait object to Box<dyn Any>.