This crates provides simplified conversion between usize
/isize
types, and types with the defined sized, depending on supported architectures.
Typically, when you want to convert usize
to u64
(or u32
) you have few options:
as
keyword. This approach may lead to incorrect resultstry_from
with unwrap
/expect
. When you target only 64-bits architectures this is fine, but produces a lot of boilerplatetry_from
and return error. This approach hides misbehavior of your code. This crates provides two features: arch-32
and arch-64
. It defines the minimum supported architecture. If you try to compile for unsupported architecture, compilation will fail with the error.
Since minimum supported pointer width is defined, we can use safe conversions for types with specific size.
```rust use arch_into::{ArchInto, ArchFrom};
fn main() { let a: u64 = 23; let b: usize = a.archinto(); let _c = u64::archfrom(b); } ```