Skip to main content

bnum/
lib.rs

1#![cfg_attr(feature = "nightly", allow(incomplete_features))]
2#![cfg_attr(
3    feature = "nightly",
4    feature(
5        generic_const_exprs,
6        const_trait_impl,
7        const_option,
8    )
9)]
10#![cfg_attr(
11    test,
12    feature(
13        bigint_helper_methods,
14        int_roundings,
15        float_minimum_maximum,
16        wrapping_next_power_of_two,
17        float_next_up_down,
18        unchecked_shifts,
19        integer_sign_cast,
20        num_midpoint_signed,
21        strict_overflow_ops,
22    )
23)]
24#![doc = include_str!("../README.md")]
25#![cfg_attr(not(any(feature = "arbitrary", feature = "quickcheck")), no_std)]
26// TODO: MAKE SURE NO_STD IS ENABLED WHEN PUBLISHING NEW VERSION
27
28#[macro_use]
29extern crate alloc;
30
31mod bint;
32mod buint;
33
34pub mod cast;
35mod digit;
36mod doc;
37pub mod errors;
38mod int;
39mod helpers;
40mod nightly;
41pub mod prelude;
42
43#[cfg(feature = "rand")]
44pub mod random;
45
46pub mod types;
47
48// #[cfg(feature = "float")]
49// mod float;
50
51// #[cfg(feature = "float")]
52// pub use float::Float;
53
54#[cfg(test)]
55mod test;
56
57#[cfg(test)]
58use test::types::*;
59
60type ExpType = u32;
61
62mod bigints {
63    pub use crate::bint::{BInt, BIntD16, BIntD32, BIntD8};
64    pub use crate::buint::{BUint, BUintD16, BUintD32, BUintD8};
65}
66
67pub use bigints::*;
68
69macro_rules! macro_impl {
70    ($name: ident) => {
71        #[allow(unused_imports)]
72        use crate::bigints::*;
73
74        crate::main_impl!($name);
75    };
76}
77
78pub(crate) use macro_impl;
79
80macro_rules! main_impl {
81    ($name: ident) => {
82        $name!(BUint, BInt, u64);
83        $name!(BUintD32, BIntD32, u32);
84        $name!(BUintD16, BIntD16, u16);
85        $name!(BUintD8, BIntD8, u8);
86    };
87}
88
89use crate::buint::cast::buint_as_different_digit_bigint;
90use crate::bint::cast::bint_as_different_digit_bigint;
91
92buint_as_different_digit_bigint!(BUint, BInt, u64; (BUintD32, u32), (BUintD16, u16), (BUintD8, u8));
93buint_as_different_digit_bigint!(BUintD32, BIntD32, u32; (BUint, u64), (BUintD16, u16), (BUintD8, u8));
94buint_as_different_digit_bigint!(BUintD16, BIntD16, u16; (BUint, u64), (BUintD32, u32), (BUintD8, u8));
95buint_as_different_digit_bigint!(BUintD8, BIntD8, u8; (BUint, u64), (BUintD32, u32), (BUintD16, u16));
96
97bint_as_different_digit_bigint!(BUint, BInt, u64; (BIntD32, u32), (BIntD16, u16), (BIntD8, u8));
98bint_as_different_digit_bigint!(BUintD32, BIntD32, u32; (BInt, u64), (BIntD16, u16), (BIntD8, u8));
99bint_as_different_digit_bigint!(BUintD16, BIntD16, u16; (BInt, u64), (BIntD32, u32), (BIntD8, u8));
100bint_as_different_digit_bigint!(BUintD8, BIntD8, u8; (BInt, u64), (BIntD32, u32), (BIntD16, u16));
101
102pub(crate) use main_impl;
103
104/// Trait for fallible conversions between `bnum` integer types.
105/// 
106/// 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.
107pub trait BTryFrom<T>: Sized {
108    type Error;
109
110    fn try_from(from: T) -> Result<Self, Self::Error>;
111}