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, BalancedMod, BalancedModAssign, BalancedModPowerOf2, BalancedModPowerOf2Assign,
11    CeilingDivAssignMod, CeilingDivMod, CeilingMod, CeilingModAssign, CeilingModPowerOf2,
12    CeilingModPowerOf2Assign, CheckedAbs, ExtendedGcd, NegAssign, OverflowingAbs,
13    OverflowingAbsAssign, SaturatingAbs, SaturatingAbsAssign, SaturatingNeg, SaturatingNegAssign,
14    UnsignedAbs, WrappingAbs, WrappingAbsAssign,
15};
16use crate::num::basic::integers::PrimitiveInt;
17use crate::num::basic::traits::NegativeOne;
18use crate::num::comparison::traits::{EqAbs, OrdAbs, OrdAbsDouble, PartialOrdAbs};
19use crate::num::logic::traits::CheckedHammingDistance;
20#[cfg(feature = "random")]
21use crate::num::random::{HasRandomSignedRange, RandomSignedChunkable};
22use core::ops::Neg;
23
24/// The bounds that [`PrimitiveSigned`] has only when the `random` feature is enabled.
25///
26/// With the feature on these are [`HasRandomSignedRange`] and [`RandomSignedChunkable`]; with it
27/// off there are none. Every type meeting the bounds implements it automatically, so it never needs
28/// to be implemented by hand.
29///
30/// See [`PrimitiveIntRandomBounds`](crate::num::basic::integers::PrimitiveIntRandomBounds) for why
31/// this indirection exists.
32#[cfg(feature = "random")]
33pub trait PrimitiveSignedRandomBounds: HasRandomSignedRange + RandomSignedChunkable {}
34
35#[cfg(feature = "random")]
36impl<T: HasRandomSignedRange + RandomSignedChunkable> PrimitiveSignedRandomBounds for T {}
37
38/// The bounds that [`PrimitiveSigned`] has only when the `random` feature is enabled.
39///
40/// With the feature off, as here, there are none.
41#[cfg(not(feature = "random"))]
42pub trait PrimitiveSignedRandomBounds {}
43
44#[cfg(not(feature = "random"))]
45impl<T> PrimitiveSignedRandomBounds for T {}
46
47/// Defines functions on primitive signed integer types: ixx and isize.
48pub trait PrimitiveSigned:
49    Abs<Output = Self>
50    + AbsAssign
51    + BalancedMod<Self, Output = Self>
52    + BalancedModAssign<Self>
53    + BalancedModPowerOf2<Output = Self>
54    + BalancedModPowerOf2Assign
55    + CeilingDivAssignMod<Self, ModOutput = Self>
56    + CeilingDivMod<Self, DivOutput = Self, ModOutput = Self>
57    + CeilingMod<Self, Output = Self>
58    + CeilingModAssign<Self>
59    + CeilingModPowerOf2<Output = Self>
60    + CeilingModPowerOf2Assign
61    + CheckedAbs<Output = Self>
62    + CheckedHammingDistance
63    + EqAbs<Self>
64    + ExtendedGcd<Self, Cofactor = Self>
65    + From<i8>
66    + Neg<Output = Self>
67    + NegAssign
68    + NegativeOne
69    + OrdAbs
70    + OrdAbsDouble<Self>
71    + OverflowingAbs<Output = Self>
72    + OverflowingAbsAssign
73    + PartialOrdAbs<Self>
74    + PrimitiveInt
75    + PrimitiveSignedRandomBounds
76    + SaturatingAbs<Output = Self>
77    + SaturatingAbsAssign
78    + SaturatingNeg<Output = Self>
79    + SaturatingNegAssign
80    + UnsignedAbs
81    + WrappingAbs<Output = Self>
82    + WrappingAbsAssign
83{
84}
85
86/// Defines basic trait implementations for signed types.
87macro_rules! impl_basic_traits {
88    ($s: ident) => {
89        impl PrimitiveSigned for $s {}
90
91        /// The constant -1.
92        ///
93        /// # Examples
94        /// See [here](self).
95        impl NegativeOne for $s {
96            const NEGATIVE_ONE: $s = -1;
97        }
98    };
99}
100apply_to_signeds!(impl_basic_traits);