Crate epsi

Source
Expand description

§Equisized (primitive) signed ints for primitive ints

According to Rust’s reference, primitive numeric integer types in Rust are such:

§Numeric types

§Integer types

The unsigned integer types consist of:

TypeMinimumMaximum
u8028-1
u160216-1
u320232-1
u640264-1
u12802128-1

The signed two’s complement integer types consist of:

TypeMinimumMaximum
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, and isize 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.

§Why this trait is needed

All primitive numeric integer types, including machine-dependent types, come with known size that can be obtained via core::mem::size_of<T>() and the corresponding signed or unsigned counterpart with the exact size. Such algorithms as C++ 20 standard midpoint relies both on equisized primitive unsigned integers and on equisized primitive signed integers. This crate offers the latter.

§Signed integers

TypeSizeEquisized primitive signed integer
i81 bytei8
i162 bytesi16
i324 bytesi32
i648 bytesi64
i12816 bytesi128
isizeplatform-dependentisize

§Unsigned integers

TypeSizeEquisized primitive signed integer
u81 bytei8
u162 bytesi16
u324 bytesi32
u648 bytesi64
u12816 bytesi128
usizeplatform-dependentisize

§Example

You can notice that EquisizedPrimitiveSignedIntExt is quite long to type. To make it shorter, you are advised to rename the imported trait as EPSI, the namesake for the crate. Because its uses are meant to be accompanied with fully qualified syntax, such shorthand is indispensible.

  use epsi::EquisizedPrimitiveSignedIntExt as EPSI;
  let a: u8 = u8::MAX;
  assert_eq!(a as <u8 as EPSI>::EquisizedPrimitiveSignedInt, -1i8);

§Analogues

§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.

Traits§

EquisizedPrimitiveSignedIntExt
Extension trait offering EquisizedPrimitiveSignedInt type.