Expand description
§AutoSized Int Macros
This crate provides a set of procedural macros that automatically select the smallest integer type capable of representing a given literal.
-
auto_sized_unsigned!/auto_sized_unsigned_val!
→ Choose among unsigned integers (u8,u16,u32,u64,u128). -
auto_sized_signed!/auto_sized_signed_val!
→ Choose among signed integers (i8,i16,i32,i64,i128). -
auto_sized_int!/auto_sized_int_val!
→ If the literal is negative, a signed type is chosen.
If the literal is non‑negative, an unsigned type is chosen.
The accepted input range is the fulli128domain (notu128).
§Type vs. Value Macros
*_unsigned!,*_signed!,*_int!→ expand to a type.*_valvariants → expand to a value (with an explicitascast).
§Examples
use autosized_num::*;
// Type macros
type T1 = auto_sized_unsigned!(300); // expands to u16
type T2 = auto_sized_signed!(-200); // expands to i16
type T3 = auto_sized_int!(10); // expands to u8
type T4 = auto_sized_int!(-10); // expands to i8
// Value macros
let a = auto_sized_unsigned_val!(300); // 300u16
let b = auto_sized_signed_val!(-200); // -200i16
let c = auto_sized_int_val!(10); // 10u8
let d = auto_sized_int_val!(-10); // -10i8§Intended Use Cases
- Binary parsing or serialization where minimal integer widths matter.
- Defining constants or generic parameters with the smallest fitting type.
- Compile‑time tests ensuring literals map to the expected integer type.
§Notes
auto_sized_int!andauto_sized_int_val!accept the fulli128range.- Non‑integer inputs will trigger a
compile_error!.
Macros§
- auto_
sized_ int - Returns the smallest integer type (signed or unsigned) that can represent the given literal.
- auto_
sized_ int_ val - Returns the given literal as a value, cast to the smallest integer type (signed or unsigned) that can represent it.
- auto_
sized_ signed - Returns the smallest signed integer type (
i8,i16,i32,i64, ori128) that can represent the given literal. - auto_
sized_ signed_ val - Returns the given literal as a value, cast to the smallest signed integer type that can represent it.
- auto_
sized_ unsigned - Returns the smallest unsigned integer type (
u8,u16,u32,u64, oru128) that can represent the given literal. - auto_
sized_ unsigned_ val - Returns the given literal as a value, cast to the smallest unsigned integer type that can represent it.