malachite_base/num/basic/
signeds.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::num::arithmetic::traits::{
10    Abs, AbsAssign, CeilingDivAssignMod, CeilingDivMod, CeilingMod, CeilingModAssign,
11    CeilingModPowerOf2, CeilingModPowerOf2Assign, CheckedAbs, ExtendedGcd, NegAssign,
12    OverflowingAbs, OverflowingAbsAssign, SaturatingAbs, SaturatingAbsAssign, SaturatingNeg,
13    SaturatingNegAssign, UnsignedAbs, WrappingAbs, WrappingAbsAssign,
14};
15use crate::num::basic::integers::PrimitiveInt;
16use crate::num::basic::traits::NegativeOne;
17use crate::num::logic::traits::CheckedHammingDistance;
18#[cfg(feature = "random")]
19use crate::num::random::{HasRandomSignedRange, RandomSignedChunkable};
20use core::ops::Neg;
21
22// When the `random` feature is enabled, the HasRandomSignedRange and RandomSignedChunkable bounds
23// are included.
24
25#[cfg(feature = "random")]
26/// Defines functions on primitive signed integer types: ixx and isize.
27pub trait PrimitiveSigned:
28    Abs<Output = Self>
29    + AbsAssign
30    + CeilingDivAssignMod<Self, ModOutput = Self>
31    + CeilingDivMod<Self, DivOutput = Self, ModOutput = Self>
32    + CeilingMod<Self, Output = Self>
33    + CeilingModAssign<Self>
34    + CeilingModPowerOf2<Output = Self>
35    + CeilingModPowerOf2Assign
36    + CheckedAbs<Output = Self>
37    + CheckedHammingDistance
38    + ExtendedGcd<Self, Cofactor = Self>
39    + From<i8>
40    + HasRandomSignedRange
41    + Neg<Output = Self>
42    + NegAssign
43    + NegativeOne
44    + OverflowingAbs<Output = Self>
45    + OverflowingAbsAssign
46    + PrimitiveInt
47    + RandomSignedChunkable
48    + SaturatingAbs<Output = Self>
49    + SaturatingAbsAssign
50    + SaturatingNeg<Output = Self>
51    + SaturatingNegAssign
52    + UnsignedAbs
53    + WrappingAbs<Output = Self>
54    + WrappingAbsAssign
55{
56}
57
58#[cfg(not(feature = "random"))]
59/// Defines functions on primitive signed integer types: ixx and isize.
60pub trait PrimitiveSigned:
61    Abs<Output = Self>
62    + AbsAssign
63    + CeilingDivAssignMod<Self, ModOutput = Self>
64    + CeilingDivMod<Self, DivOutput = Self, ModOutput = Self>
65    + CeilingMod<Self, Output = Self>
66    + CeilingModAssign<Self>
67    + CeilingModPowerOf2<Output = Self>
68    + CeilingModPowerOf2Assign
69    + CheckedAbs<Output = Self>
70    + CheckedHammingDistance
71    + ExtendedGcd<Self, Cofactor = Self>
72    + From<i8>
73    + Neg<Output = Self>
74    + NegAssign
75    + NegativeOne
76    + OverflowingAbs<Output = Self>
77    + OverflowingAbsAssign
78    + PrimitiveInt
79    + SaturatingAbs<Output = Self>
80    + SaturatingAbsAssign
81    + SaturatingNeg<Output = Self>
82    + SaturatingNegAssign
83    + UnsignedAbs
84    + WrappingAbs<Output = Self>
85    + WrappingAbsAssign
86{
87}
88
89/// Defines basic trait implementations for signed types.
90macro_rules! impl_basic_traits {
91    ($s: ident) => {
92        impl PrimitiveSigned for $s {}
93
94        /// The constant -1.
95        ///
96        /// # Examples
97        /// See [here](self).
98        impl NegativeOne for $s {
99            const NEGATIVE_ONE: $s = -1;
100        }
101    };
102}
103apply_to_signeds!(impl_basic_traits);