Skip to main content

fastnum/bint/uint/
convert.rs

1mod from_float;
2mod from_int;
3mod from_uint;
4mod to_int;
5
6use from_float::*;
7use from_int::*;
8use from_uint::*;
9use to_int::*;
10
11use bnum::BUint;
12use core::str::from_utf8_unchecked;
13
14#[cfg(not(feature = "std"))]
15use alloc::{string::String, vec::Vec};
16
17use crate::bint::{
18    convert, doc,
19    error::from_int_error_kind,
20    intrinsics::{ExpType, *},
21    utils::types::*,
22    ParseError, UInt,
23};
24
25impl<const N: usize> UInt<N> {
26    #[inline(always)]
27    #[doc = doc::convert::from!(u64 U 256)]
28    pub const fn from_u64(n: u64) -> Self {
29        Self(BUint::from_digit(n))
30    }
31
32    convert::from_str::from_str_impl!(UInt, U, BUint);
33
34    convert::from_bytes::from_bytes_impl!(UInt, U, BUint);
35
36    from_uint_impl!(
37        from_u8 <- u8,
38        from_u16 <- u16,
39        from_u32 <- u32,
40        from_usize <- usize
41    );
42
43    try_from_uint_impl!(
44        from_u128 <- u128
45    );
46
47    try_from_int_impl!(
48        from_i8 <- i8 (from_u8 <- u8),
49        from_i16 <- i16 (from_u16 <- u16),
50        from_i32 <- i32 (from_u32 <- u32),
51        from_i64 <- i64 (from_u64 <- u64),
52        from_isize <- isize (from_usize <- usize),
53        from_i128 <- i128 (#TRY from_u128 <- u128)
54    );
55
56    from_float_impl!(from_f32, f32, decode_f32, u32_bits, from_u32);
57    from_float_impl!(from_f64, f64, decode_f64, u64_bits, from_u64);
58}
59
60impl<const N: usize> UInt<N> {
61    convert::to_str::to_str_impl!(UInt, U, BUint);
62
63    convert::to_bytes::to_bytes_impl!(UInt, U, BUint);
64
65    to_int_impl!(
66        to_i8 -> i8,
67        to_i16 -> i16,
68        to_i32 -> i32,
69        to_i64 -> i64,
70        to_i128 -> i128,
71        to_isize -> isize
72    );
73
74    to_int_impl!(
75        to_u8 -> u8,
76        to_u16 -> u16,
77        to_u32 -> u32,
78        to_u64 -> u64,
79        to_u128 -> u128,
80        to_usize -> usize
81    );
82}