4 releases
0.1.3 | Feb 10, 2022 |
---|---|
0.1.2 | Feb 10, 2022 |
0.1.1 | Feb 9, 2022 |
0.1.0 | Feb 4, 2022 |
#2679 in Rust patterns
13KB
204 lines
Enumeration of primitive integer kinds
This crate offers PrimIntKind
enum whose variants represent kinds of primitive integers.
According to Rust's reference, primitive numeric integer types in Rust are such:
Numeric types
Integer types
The unsigned integer types consist of:
Type | Minimum | Maximum |
---|---|---|
u8 |
0 | 28-1 |
u16 |
0 | 216-1 |
u32 |
0 | 232-1 |
u64 |
0 | 264-1 |
u128 |
0 | 2128-1 |
The signed two's complement integer types consist of:
Type | Minimum | Maximum |
---|---|---|
i8 |
-(27) | 27-1 |
i16 |
-(215) | 215-1 |
i32 |
-(231) | 231-1 |
i64 |
-(263) | 263-1 |
i128 |
-(2127) | 2127-1 |
Machine-dependent integer types
The usize
type is an unsigned integer type with the same number of bits as the
platform's pointer type. It can represent every memory address in the process.
The isize
type is a signed integer type with the same number of bits as the
platform's pointer type. The theoretical upper bound on object and array size
is the maximum isize
value. This ensures that isize
can be used to calculate
differences between pointers into an object or array and can address every byte
within an object along with one byte past the end.
usize
and isize
are at least 16-bits wide.
Note: Many pieces of Rust code may assume that pointers,
usize
, andisize
are either 32-bit or 64-bit. As a consequence, 16-bit pointer support is limited and may require explicit care and acknowledgment from a library to support.
Examples
Stable Rust
use prim_int_kind::PrimIntKind;
let res_kind = "u16".parse();
assert_eq!(res_kind, Ok(PrimIntKind::U16));
With const_trait_impl
nightly feature
Cargo.toml
# Read more about features here:
# https://doc.rust-lang.org/cargo/reference/features.html
[features]
const_trait_impl = ["prim_int_kind/const_trait_impl"]
src/main.rs
#![cfg_attr(any(doc,test,doctest, feature = "const_trait_impl"), feature(const_trait_impl))]
// Run via `cargo run --features const_trait_impl`
use core::str::FromStr;
use prim_int_kind::{PrimIntKind, PrimIntKindParsingError};
fn main() {
#[cfg(any(doc,test,doctest, feature = "const_trait_impl"))]
const res_kind: Result<PrimIntKind, PrimIntKindParsingError> = PrimIntKind::from_str("u16");
#[cfg(not(any(doc,test,doctest, feature = "const_trait_impl")))]
let res_kind: Result<PrimIntKind, PrimIntKindParsingError> = PrimIntKind::from_str("u16");
assert_eq!(res_kind, Ok(PrimIntKind::U16));
}
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Dependencies
~0.8–1.2MB
~25K SLoC