Crates.io | max_len_base_10_as_usize |
lib.rs | max_len_base_10_as_usize |
version | 0.1.0 |
source | src |
created_at | 2022-02-02 00:18:10.27635 |
updated_at | 2022-02-02 00:18:10.27635 |
description | Trait offering constant maximum lengths of primitive integers as usize |
homepage | |
repository | https://github.com/JohnScience/max_len_base_10_as_usize |
max_upload_size | |
id | 525480 |
size | 21,326 |
This crate offers MaxLenBase10AsUsize
trait that is currently implemented for all primitive integers.
According to Rust's reference, primitive numeric integer types in Rust are such:
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
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.
In order to allocate (or reserve) memory for storing stringly representations of primitive numeric types efficiently, maximum occupied capacity is needed in order to avoid reallocations and/or unnecessary copying.
You can notice that MaxLenBase10AsUsize
is quite long to type and it contains such details as numeral system and the desired type of constant. To make it shorter and more suitable for production code (as opposed to system-level or scientific code), you are advised to rename the imported trait as MaxLen
. For clarity, you may also choose to use fully qualified syntax, though it is unnecessary.
use max_len_base_10_as_usize::MaxLenBase10AsUsize as MaxLen;
assert_eq!(u8::MAX_LEN_BASE_10_AS_USIZE, <u8 as MaxLen>::MAX_LEN_BASE_10_AS_USIZE);
assert_eq!(u8::MAX_LEN_BASE_10_AS_USIZE, 3usize);