Skip to main content

malachite_base/num/basic/
signeds.rs

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