reweb3_num/
lib.rs

1#![doc = include_str!("../README.md")]
2
3#[macro_use]
4extern crate alloc;
5
6mod bint;
7mod buint;
8
9pub mod cast;
10mod digit;
11pub mod doc;
12pub mod errors;
13mod int;
14mod nightly;
15pub mod prelude;
16
17pub mod types;
18
19// #[cfg(feature = "nightly")]
20// mod float;
21
22// #[cfg(feature = "nightly")]
23// pub use float::Float;
24
25#[cfg(test)]
26mod test;
27
28#[cfg(test)]
29use test::types::*;
30
31type ExpType = u32;
32
33macro_rules! macro_impl {
34    ($name: ident) => {
35        use crate::bigints::*;
36
37        crate::main_impl!($name);
38    };
39}
40
41pub(crate) use macro_impl;
42
43macro_rules! main_impl {
44    ($name: ident) => {
45        $name!(BUint, BInt, u64);
46        $name!(BUintD32, BIntD32, u32);
47        $name!(BUintD16, BIntD16, u16);
48        $name!(BUintD8, BIntD8, u8);
49    };
50}
51
52use crate::bint::cast::bint_as_different_digit_bigint;
53use crate::buint::cast::buint_as_different_digit_bigint;
54
55buint_as_different_digit_bigint!(BUint, BInt, u64; (BUintD32, u32), (BUintD16, u16), (BUintD8, u8));
56buint_as_different_digit_bigint!(BUintD32, BIntD32, u32; (BUint, u64), (BUintD16, u16), (BUintD8, u8));
57buint_as_different_digit_bigint!(BUintD16, BIntD16, u16; (BUint, u64), (BUintD32, u32), (BUintD8, u8));
58buint_as_different_digit_bigint!(BUintD8, BIntD8, u8; (BUint, u64), (BUintD32, u32), (BUintD16, u16));
59
60bint_as_different_digit_bigint!(BUint, BInt, u64; (BIntD32, u32), (BIntD16, u16), (BIntD8, u8));
61bint_as_different_digit_bigint!(BUintD32, BIntD32, u32; (BInt, u64), (BIntD16, u16), (BIntD8, u8));
62bint_as_different_digit_bigint!(BUintD16, BIntD16, u16; (BInt, u64), (BIntD32, u32), (BIntD8, u8));
63bint_as_different_digit_bigint!(BUintD8, BIntD8, u8; (BInt, u64), (BIntD32, u32), (BIntD16, u16));
64
65pub(crate) use main_impl;
66
67mod bigints {
68    pub use crate::bint::{BInt, BIntD16, BIntD32, BIntD8};
69    pub use crate::buint::{BUint, BUintD16, BUintD32, BUintD8};
70}
71
72pub use bigints::*;
73
74/// Trait for fallible conversions between `bnum` integer types.
75///
76/// Unfortunately, [`TryFrom`] cannot currently be used for conversions between `bnum` integers, since [`TryFrom<T> for T`](https://doc.rust-lang.org/std/convert/trait.TryFrom.html#impl-TryFrom%3CU%3E-for-T) is already implemented by the standard library (and so it is not possible to implement `TryFrom<BUint<M>> for BUint<N>`). When the [`generic_const_exprs`](https://github.com/rust-lang/rust/issues/76560) feature becomes stabilised, it may be possible to use [`TryFrom`] instead of `BTryFrom`. `BTryFrom` is designed to have the same behaviour as [`TryFrom`] for conversions between two primitive types, and conversions between a primitive type and a bnum type. `BTryFrom` is a workaround for the issue described above, and so you should not implement it yourself. It should only be used for conversions between `bnum` integers.
77pub trait BTryFrom<T>: Sized {
78    type Error;
79
80    fn try_from(from: T) -> Result<Self, Self::Error>;
81}