Expand description
Integer types that have a logical size measured in bits.
This crate provides the UBitint
trait and 128 types named
U1
through U128
that implement it. Each type
wraps the smallest primitive unsigned integer type that can contain it. The
types that are not the same width as their primitive type impose a validity
constraint—the value is represented in the least significant bits and the
upper bits are always clear.
Demo
// Recommended, but not required.
use bitint::prelude::*;
// Use the bitint! macro to write a bitint literal. Underscores are
// permitted anywhere in a Rust literal and are encouraged for readability.
let seven = bitint!(7_U12);
// Use the #[bitint_literals] attribute macro to write bitint literals
// anywhere inside an item. Here the item is a function, but it can also be
// useful on an impl block or inline module.
#[bitint_literals]
fn demo() {
let five = 5_U12;
// Arithmetic ops accept Self or the primitive type and panic or wrap
// just like primitive arithmetic ops.
assert_eq!(five + five, 10_U12);
assert_eq!(five - 1_U12, 4_U12);
assert_eq!(five * 2_U12, 10_U12);
assert_eq!(five / 3_U12, 1_U12);
assert_eq!(five % 3_U12, 2_U12);
// If built with overflow-checks = true, this would panic.
// If built with overflow-checks = false, this would wrap.
// five + U12::MAX
// Checked arithmetic ops.
assert_eq!(five.checked_add(10_U12), Some(15_U12));
assert_eq!(five.checked_add(4095_U12), None);
// Wrapping arithmetic ops.
assert_eq!(five.wrapping_add(10_U12), 15_U12);
assert_eq!(five.wrapping_add(4095_U12), 4_U12);
// Zero-(extra)-cost unchecked arithmetic ops.
#[cfg(feature = "unchecked_math")]
{
// SAFETY: 15 is in range for U12.
assert_eq!(unsafe { five.unchecked_add(10_U12) }, 15_U12);
// This would violate the safety condition and cause undefined
// behavior.
// unsafe { five.unchecked_add(4095_U12) }
}
// Zero-cost conversion to a primitive type.
assert_eq!(five.to_primitive(), 5);
// Checked constructor.
assert_eq!(U12::new(5), Some(five));
assert_eq!(U12::new(4096), None);
// Masking constructor.
assert_eq!(U12::new_masked(5), five);
assert_eq!(U12::new_masked(13 * 4096 + 5), five);
// Zero-cost unsafe constructor.
// SAFETY: 5 is in range for U12.
assert_eq!(unsafe { U12::new_unchecked(5) }, five);
// This would violate the safety condition and cause undefined behavior.
// unsafe { U12::new_unchecked(65536) }
// Zero-cost safe constructor, only for bitints that are the same width
// as a primitive type.
assert_eq!(U16::from_primitive(1234), 1234_U16);
// String conversions.
assert_eq!(format!("{five}"), "5");
assert_eq!(five.to_string(), "5");
assert_eq!("5".parse::<U12>().unwrap(), 5_U12);
};
Crate features
- unchecked_math - Enables the
unchecked_*
methods on unsignedbitint
types. Requires a nightly Rust compiler.
Modules
- Convenience re-exports.
Macros
- Constructs a
bitint
literal.
Structs
- The error type returned when a
TryFrom
conversion to abitint
fails. - The 1-bit unsigned
bitint
type. - The 2-bit unsigned
bitint
type. - The 3-bit unsigned
bitint
type. - The 4-bit unsigned
bitint
type. - The 5-bit unsigned
bitint
type. - The 6-bit unsigned
bitint
type. - The 7-bit unsigned
bitint
type. - The 8-bit unsigned
bitint
type. - The 9-bit unsigned
bitint
type. - The 10-bit unsigned
bitint
type. - The 11-bit unsigned
bitint
type. - The 12-bit unsigned
bitint
type. - The 13-bit unsigned
bitint
type. - The 14-bit unsigned
bitint
type. - The 15-bit unsigned
bitint
type. - The 16-bit unsigned
bitint
type. - The 17-bit unsigned
bitint
type. - The 18-bit unsigned
bitint
type. - The 19-bit unsigned
bitint
type. - The 20-bit unsigned
bitint
type. - The 21-bit unsigned
bitint
type. - The 22-bit unsigned
bitint
type. - The 23-bit unsigned
bitint
type. - The 24-bit unsigned
bitint
type. - The 25-bit unsigned
bitint
type. - The 26-bit unsigned
bitint
type. - The 27-bit unsigned
bitint
type. - The 28-bit unsigned
bitint
type. - The 29-bit unsigned
bitint
type. - The 30-bit unsigned
bitint
type. - The 31-bit unsigned
bitint
type. - The 32-bit unsigned
bitint
type. - The 33-bit unsigned
bitint
type. - The 34-bit unsigned
bitint
type. - The 35-bit unsigned
bitint
type. - The 36-bit unsigned
bitint
type. - The 37-bit unsigned
bitint
type. - The 38-bit unsigned
bitint
type. - The 39-bit unsigned
bitint
type. - The 40-bit unsigned
bitint
type. - The 41-bit unsigned
bitint
type. - The 42-bit unsigned
bitint
type. - The 43-bit unsigned
bitint
type. - The 44-bit unsigned
bitint
type. - The 45-bit unsigned
bitint
type. - The 46-bit unsigned
bitint
type. - The 47-bit unsigned
bitint
type. - The 48-bit unsigned
bitint
type. - The 49-bit unsigned
bitint
type. - The 50-bit unsigned
bitint
type. - The 51-bit unsigned
bitint
type. - The 52-bit unsigned
bitint
type. - The 53-bit unsigned
bitint
type. - The 54-bit unsigned
bitint
type. - The 55-bit unsigned
bitint
type. - The 56-bit unsigned
bitint
type. - The 57-bit unsigned
bitint
type. - The 58-bit unsigned
bitint
type. - The 59-bit unsigned
bitint
type. - The 60-bit unsigned
bitint
type. - The 61-bit unsigned
bitint
type. - The 62-bit unsigned
bitint
type. - The 63-bit unsigned
bitint
type. - The 64-bit unsigned
bitint
type. - The 65-bit unsigned
bitint
type. - The 66-bit unsigned
bitint
type. - The 67-bit unsigned
bitint
type. - The 68-bit unsigned
bitint
type. - The 69-bit unsigned
bitint
type. - The 70-bit unsigned
bitint
type. - The 71-bit unsigned
bitint
type. - The 72-bit unsigned
bitint
type. - The 73-bit unsigned
bitint
type. - The 74-bit unsigned
bitint
type. - The 75-bit unsigned
bitint
type. - The 76-bit unsigned
bitint
type. - The 77-bit unsigned
bitint
type. - The 78-bit unsigned
bitint
type. - The 79-bit unsigned
bitint
type. - The 80-bit unsigned
bitint
type. - The 81-bit unsigned
bitint
type. - The 82-bit unsigned
bitint
type. - The 83-bit unsigned
bitint
type. - The 84-bit unsigned
bitint
type. - The 85-bit unsigned
bitint
type. - The 86-bit unsigned
bitint
type. - The 87-bit unsigned
bitint
type. - The 88-bit unsigned
bitint
type. - The 89-bit unsigned
bitint
type. - The 90-bit unsigned
bitint
type. - The 91-bit unsigned
bitint
type. - The 92-bit unsigned
bitint
type. - The 93-bit unsigned
bitint
type. - The 94-bit unsigned
bitint
type. - The 95-bit unsigned
bitint
type. - The 96-bit unsigned
bitint
type. - The 97-bit unsigned
bitint
type. - The 98-bit unsigned
bitint
type. - The 99-bit unsigned
bitint
type. - The 100-bit unsigned
bitint
type. - The 101-bit unsigned
bitint
type. - The 102-bit unsigned
bitint
type. - The 103-bit unsigned
bitint
type. - The 104-bit unsigned
bitint
type. - The 105-bit unsigned
bitint
type. - The 106-bit unsigned
bitint
type. - The 107-bit unsigned
bitint
type. - The 108-bit unsigned
bitint
type. - The 109-bit unsigned
bitint
type. - The 110-bit unsigned
bitint
type. - The 111-bit unsigned
bitint
type. - The 112-bit unsigned
bitint
type. - The 113-bit unsigned
bitint
type. - The 114-bit unsigned
bitint
type. - The 115-bit unsigned
bitint
type. - The 116-bit unsigned
bitint
type. - The 117-bit unsigned
bitint
type. - The 118-bit unsigned
bitint
type. - The 119-bit unsigned
bitint
type. - The 120-bit unsigned
bitint
type. - The 121-bit unsigned
bitint
type. - The 122-bit unsigned
bitint
type. - The 123-bit unsigned
bitint
type. - The 124-bit unsigned
bitint
type. - The 125-bit unsigned
bitint
type. - The 126-bit unsigned
bitint
type. - The 127-bit unsigned
bitint
type. - The 128-bit unsigned
bitint
type.
Enums
- The error type returned when parsing a string to a
bitint
fails. - Maps each bit width to its corresponding
UBitint
type.
Traits
- A type-level function returning a
UBitint
. - Unsigned
bitint
types.
Attribute Macros
- Rewrites
bitint
literals in the item it is attached to.