1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! This crate provides new integer types with non-standard and fixed bitwidths
//! such as `U24`, `I48`, `U96` and so forth with a focus on data layout and alignment.
//!
//! - All integers provided by this crate require the minimum number of bytes for their representation.
//! For example, `U24` requires 3 bytes, `I48` requires 6 bytes.
//! - The alignment of all integer types provided by this crate is always 1. If another
//! alignment is required it is recommended to wrap the integer type in a newtype and
//! enforce an alignment via `#[align(N)]`.
//! - As of now the provided integers do not have a rich set of arithmetic methods defined on them.
//! It is instead expected to convert them to Rust primitive integers, apply the computation and
//! eventually convert the result back. This might be supported in the future if requested.
//! - The binary representation of integer types provided by this crate is in twos-complement just
//! like Rust's built-in integer types.
//!
//! ## Data Layout
//!
//! All integer types provided by this crate internally consists of a single byte array.
//! For example, the structure of `U24` is `struct U24([u8; 3]);` allowing for optimal memory
//! usage and an alignment of 1 (if needed).
//!
//! ## API
//!
//! Integer types provided by this crate only have very a minimal API surface.
//!
//! - Traits implemented by all of the integer types are the following:
//!
//! - `Clone`, `Copy`, `Default`, `Eq`, `PartialEq`, `Ord`, `PartialOrd`, `Hash`
//! - Common traits are all implemented as efficiently as possible for every integer type.
//! - `Debug`, `Display`, `Binary`, `Octal`, `LowerHex`, `UpperHex`, `LowerExp`, `UpperExp`
//! - Integer types mimick the display representation of the next larger Rust built-in integer type.
//!
//! - Endian-aware conversion routines are also implemented:
//!
//! - `from_ne_bytes`, `to_ne_bytes`: Convert from and to native-endian bytes. (always efficient)
//! - `from_le_bytes`, `to_le_bytes`: Convert from and to little-endian bytes.
//! - `from_be_bytes`, `to_be_bytes`: Convert from and to big-endian bytes.
//!
//! - Rich `From` and `TryFrom` implementations:
//!
//! - All provided integer types have a very rich set of `From` and `TryFrom` trait implementations
//! to efficiently convert between different integer types and Rust built-in integers.
//!
//!
//! # Example: Packed
//!
//! Here the Rust compiler wastes 3 bytes for the discriminent of the `enum`.
//! ```
//! pub enum Unpacked {
//! A(u32), // We only use the lowest 24-bit of the integer.
//! B(i16),
//! }
//! assert_eq!(core::mem::size_of::<Unpacked>(), 8);
//! ```
//!
//! Using `intx::U24` the Rust compiler can properly pack the `enum` type wasting no bytes.
//! ```
//! pub enum Packed {
//! A(intx::U24), // Now using a type that reflects our usage intent properly.
//! B(i16),
//! }
//! assert_eq!(core::mem::size_of::<Packed>(), 4);
//! ```
//!
//! # Example: Alignment
//!
//! With standard alignment the `enum` discriminent takes up a whopping 8 bytes.
//! ```
//! pub enum Aligned {
//! A(u64),
//! B(i64),
//! }
//! assert_eq!(core::mem::size_of::<Aligned>(), 16);
//! ```
//!
//! Using `intx` integers with their alignment of 1 allows to pack the `enum` discrimant to a single byte.
//! ```
//! pub enum Unaligned {
//! A(intx::U64),
//! B(intx::I64),
//! }
//! assert_eq!(core::mem::size_of::<Unaligned>(), 9);
//! ```
pub use ;
pub use TryFromIntError;
pub use IsWithinBoundsOf;
/// Trait implemented by Rust integer primitives to communicate their bounds.
impl_bounded_integer_for!;
/// Trait implemented by unaligned integers provided by this crate.
impl_unaligned_uint_for!;
impl_unaligned_int_for!;