#signed-integer #integer #negative #positive #signed #bounded

sign-bound

Layout-optimized positive and negative integers

1 stable release

new 2.1.1 Dec 2, 2024
2.0.0 Nov 25, 2024
1.0.0 Sep 21, 2024

#980 in Rust patterns

Download history 211/week @ 2024-09-20 35/week @ 2024-09-27 6/week @ 2024-10-04 2/week @ 2024-10-11 144/week @ 2024-11-22 269/week @ 2024-11-29

413 downloads per month

MIT license

62KB
1.5K SLoC

sign-bound   Latest Version Documentation

Signed integer types for Rust that are bounded to be either positive or negative. The API is analogous to the built-in NonZero types:

  • PositiveI8, NegativeI8
  • PositiveI16, NegativeI16
  • PositiveI32, NegativeI32
  • PositiveI64, NegativeI64
  • PositiveIsize, NegativeIsize

The types are all memory-layout optimized, so for example Option<PositiveI32> and Option<NegativeI32> are both the same size as i32. Using additional variants in an enum can also have some space benefits.

enum MyEnum {
    A(PositiveI16),
    B,
    C,
    D,
}
assert_eq!(size_of::<MyEnum>(), size_of::<PositiveI16>());

Note that due to the implementation details of this crate, the space optimization for any type will not occur if there are more than 128 additional enum variants.

Option<PositiveIsize> is particularly useful as a space-efficient optional Vec index, since Rust's Vec structure is limited to isize::MAX entries.

No runtime deps