#fixed-size #conversion #architecture #pointers #64-bit

arch-into

safe type conversions between pointer-sized types (usize/isize) and types with fixed size

5 releases

0.0.1-alpha.5 Aug 9, 2023
0.0.1-alpha.4 Apr 7, 2023
0.0.1-alpha.3 Apr 6, 2023

#758 in Rust patterns

Download history 1648/week @ 2024-06-10 2667/week @ 2024-06-17 3695/week @ 2024-06-24 2353/week @ 2024-07-01 5436/week @ 2024-07-08 3205/week @ 2024-07-15 3519/week @ 2024-07-22 2743/week @ 2024-07-29 3250/week @ 2024-08-05 3667/week @ 2024-08-12 2635/week @ 2024-08-19 2578/week @ 2024-08-26 3158/week @ 2024-09-02 1986/week @ 2024-09-09 2002/week @ 2024-09-16 1555/week @ 2024-09-23

8,889 downloads per month

MIT/Apache

7KB
87 lines

arch-into

This crate provides simplified conversions between usize/isize types, and types with constant size, depending on supported architectures.

Typically, when you want to convert usize to u64 (or u32) you have few options:

  • Use as keyword. This approach may lead to incorrect results
  • Use try_from with unwrap/expect. When you target only 64-bits architectures this is fine, but it produces a lot of boilerplate
  • Use try_from and return error. This approach hides misbehavior of your code.

This crates provides two features: no-arch-32 and no-arch-64. If you try to compile for unsupported architecture, compilation will fail with the error.

Since unsupported pointer width is defined, we can use safe conversions for types with specific size.

Usage

use arch_into::{ArchInto, ArchFrom};

fn main() {
    let a: u64 = 23;
    let b: usize = a.arch_into();
    let _c = u64::arch_from(b);
}

Dependencies

~220KB