Crate autosized_num

Crate autosized_num 

Source
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 full i128 domain (not u128).

§Type vs. Value Macros

  • *_unsigned!, *_signed!, *_int! → expand to a type.
  • *_val variants → expand to a value (with an explicit as cast).

§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! and auto_sized_int_val! accept the full i128 range.
  • 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, or i128) 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, or u128) 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.