Crates.io | arch-into |
lib.rs | arch-into |
version | 0.0.1-alpha.5 |
source | src |
created_at | 2023-04-06 01:18:06.388083 |
updated_at | 2023-08-09 20:19:51.340017 |
description | safe type conversions between pointer-sized types (usize/isize) and types with fixed size |
homepage | |
repository | https://github.com/glebpom/arch-into |
max_upload_size | |
id | 831872 |
size | 10,318 |
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:
as
keyword. This approach may lead to incorrect resultstry_from
with unwrap
/expect
. When you target only 64-bits architectures this is fine, but it produces a lot of boilerplatetry_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.
use arch_into::{ArchInto, ArchFrom};
fn main() {
let a: u64 = 23;
let b: usize = a.arch_into();
let _c = u64::arch_from(b);
}