Skip to content

glebpom/arch-into

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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);
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages