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 unsignedbitinttypes. Requires a nightly Rust compiler.
Modules
- Convenience re-exports.
Macros
- Constructs a
bitintliteral.
Structs
- The error type returned when a
TryFromconversion to abitintfails. - The 1-bit unsigned
bitinttype. - The 2-bit unsigned
bitinttype. - The 3-bit unsigned
bitinttype. - The 4-bit unsigned
bitinttype. - The 5-bit unsigned
bitinttype. - The 6-bit unsigned
bitinttype. - The 7-bit unsigned
bitinttype. - The 8-bit unsigned
bitinttype. - The 9-bit unsigned
bitinttype. - The 10-bit unsigned
bitinttype. - The 11-bit unsigned
bitinttype. - The 12-bit unsigned
bitinttype. - The 13-bit unsigned
bitinttype. - The 14-bit unsigned
bitinttype. - The 15-bit unsigned
bitinttype. - The 16-bit unsigned
bitinttype. - The 17-bit unsigned
bitinttype. - The 18-bit unsigned
bitinttype. - The 19-bit unsigned
bitinttype. - The 20-bit unsigned
bitinttype. - The 21-bit unsigned
bitinttype. - The 22-bit unsigned
bitinttype. - The 23-bit unsigned
bitinttype. - The 24-bit unsigned
bitinttype. - The 25-bit unsigned
bitinttype. - The 26-bit unsigned
bitinttype. - The 27-bit unsigned
bitinttype. - The 28-bit unsigned
bitinttype. - The 29-bit unsigned
bitinttype. - The 30-bit unsigned
bitinttype. - The 31-bit unsigned
bitinttype. - The 32-bit unsigned
bitinttype. - The 33-bit unsigned
bitinttype. - The 34-bit unsigned
bitinttype. - The 35-bit unsigned
bitinttype. - The 36-bit unsigned
bitinttype. - The 37-bit unsigned
bitinttype. - The 38-bit unsigned
bitinttype. - The 39-bit unsigned
bitinttype. - The 40-bit unsigned
bitinttype. - The 41-bit unsigned
bitinttype. - The 42-bit unsigned
bitinttype. - The 43-bit unsigned
bitinttype. - The 44-bit unsigned
bitinttype. - The 45-bit unsigned
bitinttype. - The 46-bit unsigned
bitinttype. - The 47-bit unsigned
bitinttype. - The 48-bit unsigned
bitinttype. - The 49-bit unsigned
bitinttype. - The 50-bit unsigned
bitinttype. - The 51-bit unsigned
bitinttype. - The 52-bit unsigned
bitinttype. - The 53-bit unsigned
bitinttype. - The 54-bit unsigned
bitinttype. - The 55-bit unsigned
bitinttype. - The 56-bit unsigned
bitinttype. - The 57-bit unsigned
bitinttype. - The 58-bit unsigned
bitinttype. - The 59-bit unsigned
bitinttype. - The 60-bit unsigned
bitinttype. - The 61-bit unsigned
bitinttype. - The 62-bit unsigned
bitinttype. - The 63-bit unsigned
bitinttype. - The 64-bit unsigned
bitinttype. - The 65-bit unsigned
bitinttype. - The 66-bit unsigned
bitinttype. - The 67-bit unsigned
bitinttype. - The 68-bit unsigned
bitinttype. - The 69-bit unsigned
bitinttype. - The 70-bit unsigned
bitinttype. - The 71-bit unsigned
bitinttype. - The 72-bit unsigned
bitinttype. - The 73-bit unsigned
bitinttype. - The 74-bit unsigned
bitinttype. - The 75-bit unsigned
bitinttype. - The 76-bit unsigned
bitinttype. - The 77-bit unsigned
bitinttype. - The 78-bit unsigned
bitinttype. - The 79-bit unsigned
bitinttype. - The 80-bit unsigned
bitinttype. - The 81-bit unsigned
bitinttype. - The 82-bit unsigned
bitinttype. - The 83-bit unsigned
bitinttype. - The 84-bit unsigned
bitinttype. - The 85-bit unsigned
bitinttype. - The 86-bit unsigned
bitinttype. - The 87-bit unsigned
bitinttype. - The 88-bit unsigned
bitinttype. - The 89-bit unsigned
bitinttype. - The 90-bit unsigned
bitinttype. - The 91-bit unsigned
bitinttype. - The 92-bit unsigned
bitinttype. - The 93-bit unsigned
bitinttype. - The 94-bit unsigned
bitinttype. - The 95-bit unsigned
bitinttype. - The 96-bit unsigned
bitinttype. - The 97-bit unsigned
bitinttype. - The 98-bit unsigned
bitinttype. - The 99-bit unsigned
bitinttype. - The 100-bit unsigned
bitinttype. - The 101-bit unsigned
bitinttype. - The 102-bit unsigned
bitinttype. - The 103-bit unsigned
bitinttype. - The 104-bit unsigned
bitinttype. - The 105-bit unsigned
bitinttype. - The 106-bit unsigned
bitinttype. - The 107-bit unsigned
bitinttype. - The 108-bit unsigned
bitinttype. - The 109-bit unsigned
bitinttype. - The 110-bit unsigned
bitinttype. - The 111-bit unsigned
bitinttype. - The 112-bit unsigned
bitinttype. - The 113-bit unsigned
bitinttype. - The 114-bit unsigned
bitinttype. - The 115-bit unsigned
bitinttype. - The 116-bit unsigned
bitinttype. - The 117-bit unsigned
bitinttype. - The 118-bit unsigned
bitinttype. - The 119-bit unsigned
bitinttype. - The 120-bit unsigned
bitinttype. - The 121-bit unsigned
bitinttype. - The 122-bit unsigned
bitinttype. - The 123-bit unsigned
bitinttype. - The 124-bit unsigned
bitinttype. - The 125-bit unsigned
bitinttype. - The 126-bit unsigned
bitinttype. - The 127-bit unsigned
bitinttype. - The 128-bit unsigned
bitinttype.
Enums
- The error type returned when parsing a string to a
bitintfails. - Maps each bit width to its corresponding
UBitinttype.
Traits
- A type-level function returning a
UBitint. - Unsigned
bitinttypes.
Attribute Macros
- Rewrites
bitintliterals in the item it is attached to.