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