malachite_nz/integer/conversion/
from_primitive_int.rs

1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::integer::Integer;
10use crate::natural::Natural;
11use crate::platform::{Limb, SignedLimb};
12
13impl Integer {
14    /// Converts a [`Limb`](crate#limbs) to an [`Integer`].
15    ///
16    /// This function is const, so it may be used to define constants.
17    ///
18    /// # Worst-case complexity
19    /// Constant time and additional memory.
20    ///
21    /// # Examples
22    /// ```
23    /// use malachite_nz::integer::Integer;
24    ///
25    /// const TEN: Integer = Integer::const_from_unsigned(10);
26    /// assert_eq!(TEN, 10);
27    /// ```
28    pub const fn const_from_unsigned(x: Limb) -> Self {
29        Self {
30            sign: true,
31            abs: Natural::const_from(x),
32        }
33    }
34
35    /// Converts a [`SignedLimb`](crate#limbs) to an [`Integer`].
36    ///
37    /// This function is const, so it may be used to define constants.
38    ///
39    /// # Worst-case complexity
40    /// Constant time and additional memory.
41    ///
42    /// # Examples
43    /// ```
44    /// use malachite_nz::integer::Integer;
45    ///
46    /// const TEN: Integer = Integer::const_from_signed(10);
47    /// assert_eq!(TEN, 10);
48    ///
49    /// const NEGATIVE_TEN: Integer = Integer::const_from_signed(-10);
50    /// assert_eq!(NEGATIVE_TEN, -10);
51    /// ```
52    pub const fn const_from_signed(x: SignedLimb) -> Self {
53        Self {
54            sign: x >= 0,
55            abs: Natural::const_from(x.unsigned_abs()),
56        }
57    }
58}
59
60macro_rules! impl_from_unsigned {
61    ($t: ident) => {
62        impl From<$t> for Integer {
63            /// Converts an unsigned primitive integer to an [`Integer`].
64            ///
65            /// # Worst-case complexity
66            /// Constant time and additional memory.
67            ///
68            /// # Examples
69            /// See [here](super::from_primitive_int#from).
70            #[inline]
71            fn from(u: $t) -> Integer {
72                Integer {
73                    sign: true,
74                    abs: Natural::from(u),
75                }
76            }
77        }
78    };
79}
80apply_to_unsigneds!(impl_from_unsigned);
81
82macro_rules! impl_from_signed {
83    ($t: ident) => {
84        impl From<$t> for Integer {
85            /// Converts a signed primitive integer to an [`Integer`].
86            ///
87            /// # Worst-case complexity
88            /// Constant time and additional memory.
89            ///
90            /// # Examples
91            /// See [here](super::from_primitive_int#from).
92            #[inline]
93            fn from(i: $t) -> Integer {
94                Integer {
95                    sign: i >= 0,
96                    abs: Natural::from(i.unsigned_abs()),
97                }
98            }
99        }
100    };
101}
102apply_to_signeds!(impl_from_signed);