Module condtype::num

source ·
Expand description

Conditional aliases to numeric types.

Examples

use std::fs::File;
use condtype::num::Usize64;

fn handle_throughput(bytes: Usize64) {
    // ...
}

// usize
let s: &str = // ...
handle_throughput(s.len() as Usize64);

// u64
let f: File = // ...
handle_throughput(f.metadata()?.len() as Usize64);

Pitfalls

Because these are type aliases, some operations may happen to work for the current target but not for other targets.

The following example handles Usize32 explicitly as usize or u32 depending on whether the platform is 64-bit or 32-bit:

#[cfg(target_pointer_width = "64")]
let x: Usize32 = usize::MAX;

#[cfg(target_pointer_width = "32")]
let x: Usize32 = u32::MAX;

Instead, the code should be made portable by using an as cast:

let x: Usize32 = usize::MAX as Usize32;

Type Definitions