num_to/
lib.rs

1#![allow(clippy::wrong_self_convention)]
2//! This crate provides traits [`To{type}`] and [`As{type}`] providing methods [`to_{type}`] and [`as_{type}`] for converting numbers between types
3//! without using [`as`] casts.  The rules for these names are as follows.  Methods named
4//! [`as_{type}`] will always convert the value losslessly producing an exact result, whereas
5//! [`to_{type]`] are possibly lossy but will always convert to the nearest value of the result type during
6//! integer-integer conversions, and for conversions involving floating-point values, the result is
7//! the same as that of an [`as`] cast.
8//! 
9//! Whenever an [`As`] trait is implemented, the corresponding [`To`] trait is also implemented,
10//! and has the same behavior.
11//! 
12//! Existence of [`AsUsize`] and [`AsIsize`] for a particular type is target architecture
13//! dependent, so usage of these traits is discouraged.
14//! 
15//! [`use num-to::*;`] will include only the 14 [`As`] traits, 14 [`To`] traits and nothing
16//! else.
17
18mod blanket;
19mod u8;
20mod u16;
21mod u32;
22mod u64;
23mod u128;
24mod usize;
25
26mod i8;
27mod i16;
28mod i32;
29mod i64;
30mod i128;
31mod isize;
32
33mod f32;
34mod f64;
35
36pub trait AsU8 { fn as_u8(self) -> u8; }
37pub trait AsU16 { fn as_u16(self) -> u16; }
38pub trait AsU32 { fn as_u32(self) -> u32; }
39pub trait AsU64 { fn as_u64(self) -> u64; }
40pub trait AsU128 { fn as_u128(self) -> u128; }
41pub trait AsUsize { fn as_usize(self) -> usize; }
42
43pub trait AsI8 { fn as_i8(self) -> i8; }
44pub trait AsI16 { fn as_i16(self) -> i16; }
45pub trait AsI32 { fn as_i32(self) -> i32; }
46pub trait AsI64 { fn as_i64(self) -> i64; }
47pub trait AsI128 { fn as_i128(self) -> i128; }
48pub trait AsIsize { fn as_isize(self) -> isize; }
49
50pub trait AsF32 { fn as_f32(self) -> f32; }
51pub trait AsF64 { fn as_f64(self) -> f64; }
52
53pub trait ToU8 { fn to_u8(self) -> u8; }
54pub trait ToU16 { fn to_u16(self) -> u16; }
55pub trait ToU32 { fn to_u32(self) -> u32; }
56pub trait ToU64 { fn to_u64(self) -> u64; }
57pub trait ToU128 { fn to_u128(self) -> u128; }
58pub trait ToUsize { fn to_usize(self) -> usize; }
59
60pub trait ToI8 { fn to_i8(self) -> i8; }
61pub trait ToI16 { fn to_i16(self) -> i16; }
62pub trait ToI32 { fn to_i32(self) -> i32; }
63pub trait ToI64 { fn to_i64(self) -> i64; }
64pub trait ToI128 { fn to_i128(self) -> i128; }
65pub trait ToIsize { fn to_isize(self) -> isize; }
66
67pub trait ToF32 { fn to_f32(self) -> f32; }
68pub trait ToF64 { fn to_f64(self) -> f64; }