Crates.io | intx |
lib.rs | intx |
version | 0.1.0 |
source | src |
created_at | 2023-05-15 12:32:09.041697 |
updated_at | 2023-05-15 12:32:09.041697 |
description | Defines new integer types with non-standard but fixed sizes. |
homepage | |
repository | https://github.com/Robbepop/intx |
max_upload_size | |
id | 864980 |
size | 115,205 |
Continuous Integration | Documentation | Crates.io |
---|---|---|
intx
- Non-standard fixed bitwidth integers for RustWARNING: This crate is not yet production ready as it is missing lots of tests.
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.
U24
requires 3 bytes, I48
requires 6 bytes.#[align(N)]
.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).
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
Debug
, Display
, Binary
, Octal
, LowerHex
, UpperHex
, LowerExp
, UpperExp
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:
From
and TryFrom
trait implementations
to efficiently convert between different integer types and Rust built-in integers.The focus of this crate is data layout, alignment and space-optization.
It was crafted as an experiment when the wasmi
interpreter
required a more memory efficient representation of its internal bytecode.
For example using a 3 bytes sized U24
instead of a 4 byte sized u32
in certain places allows the Rust
compiler to pack data structures more efficiently in some cases,
especially when using enum
types.
If your primary focus is on the logical side where you are mostly interested in
arithmetic operations on integer types with non-standard but fixed bitwidths then
maybe the ux
crate is a better fit for you.
The alternative ux
crate provides non-standard
and fixed bitwidth integers as well but the focus of both crates is very different.
Property | intx |
ux |
---|---|---|
size_of |
All integer types require the minimum number of bytes for their representation. For example, size_of<intx::U24>() == 3 |
All integer types have the same size_of as the next biggest Rust built-in integer primitive. For example, size_of<ux::u24>() == size_of<u32>() == 4 |
align_of |
All integer types have an alignment of 1. If another alignment is needed it is possible to wrap the integer type in a newtype and enforce another alignment via #[align(N)] |
All integer types have the same align_of as the next biggest Rust built-in integer primitive. For example align_of<ux::u24>() == align_of<u32> == 4 . |
Focus | Data layout and alignment of packed data structures using integers. | Arithmetic operations on non-standard bitwidth integer types. |
API | Integer types provide a minimal API surface. Mostly From and TryFrom impls between integers and Rust primitives as well as endian-aware byte conversions known from Rust primitives such as to_ne_bytes and from_le_bytes . |
Integer types try to mimick Rust built-in integer types providing a fair amount of arithmetic operations on them. |
Both crates provide rich support for conversions between different integer types and Rust primitives.
Licensed under either of
Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.