Skip to content

Commit d705f65

Browse files
authored
Merge pull request #322 from DaniPopes/more-clippies
chore: warn on `missing_docs` and `clippy::missing_inline_in_public_items`
2 parents 82970da + 9809999 commit d705f65

25 files changed

+184
-32
lines changed

src/add.rs

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
5353
/// Returns a tuple of the addition along with a boolean indicating whether
5454
/// an arithmetic overflow would occur. If an overflow would have occurred
5555
/// then the wrapped value is returned.
56+
#[inline]
5657
#[must_use]
5758
pub fn overflowing_add(mut self, rhs: Self) -> (Self, bool) {
5859
if BITS == 0 {
@@ -87,6 +88,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
8788
/// Returns a tuple of the subtraction along with a boolean indicating
8889
/// whether an arithmetic overflow would occur. If an overflow would have
8990
/// occurred then the wrapped value is returned.
91+
#[inline]
9092
#[must_use]
9193
pub fn overflowing_sub(mut self, rhs: Self) -> (Self, bool) {
9294
if BITS == 0 {
@@ -168,6 +170,7 @@ impl<const BITS: usize, const LIMBS: usize> Neg for &Uint<BITS, LIMBS> {
168170
}
169171

170172
impl<const BITS: usize, const LIMBS: usize> Sum<Self> for Uint<BITS, LIMBS> {
173+
#[inline]
171174
fn sum<I>(iter: I) -> Self
172175
where
173176
I: Iterator<Item = Self>,
@@ -177,6 +180,7 @@ impl<const BITS: usize, const LIMBS: usize> Sum<Self> for Uint<BITS, LIMBS> {
177180
}
178181

179182
impl<'a, const BITS: usize, const LIMBS: usize> Sum<&'a Self> for Uint<BITS, LIMBS> {
183+
#[inline]
180184
fn sum<I>(iter: I) -> Self
181185
where
182186
I: Iterator<Item = &'a Self>,

src/algorithms/div/knuth.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use core::u64;
1717
/// # Panics
1818
///
1919
/// May panic if the above requirements are not met.
20+
#[inline]
2021
#[allow(clippy::many_single_char_names)]
2122
pub fn div_nxm_normalized(numerator: &mut [u64], divisor: &[u64]) {
2223
debug_assert!(divisor.len() >= 2);
@@ -83,6 +84,7 @@ pub fn div_nxm_normalized(numerator: &mut [u64], divisor: &[u64]) {
8384
/// # Panics
8485
///
8586
/// May panic if the above requirements are not met.
87+
#[inline]
8688
#[allow(clippy::many_single_char_names)]
8789
pub fn div_nxm(numerator: &mut [u64], divisor: &mut [u64]) {
8890
debug_assert!(divisor.len() >= 3);

src/algorithms/gcd/matrix.rs

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ impl Matrix {
2121
pub const IDENTITY: Self = Self(1, 0, 0, 1, true);
2222

2323
/// Returns the matrix product `self * other`.
24+
#[inline]
2425
#[allow(clippy::suspicious_operation_groupings)]
2526
#[must_use]
2627
pub const fn compose(self, other: Self) -> Self {
@@ -34,6 +35,7 @@ impl Matrix {
3435
}
3536

3637
/// Applies the matrix to a `Uint`.
38+
#[inline]
3739
pub fn apply<const BITS: usize, const LIMBS: usize>(
3840
&self,
3941
a: &mut Uint<BITS, LIMBS>,
@@ -60,6 +62,7 @@ impl Matrix {
6062
}
6163

6264
/// Applies the matrix to a `u128`.
65+
#[inline]
6366
#[must_use]
6467
pub const fn apply_u128(&self, a: u128, b: u128) -> (u128, u128) {
6568
// Intermediate values can overflow but the final result will fit, so we
@@ -90,6 +93,7 @@ impl Matrix {
9093
/// # Panics
9194
///
9295
/// Panics if `b > a`.
96+
#[inline]
9397
#[must_use]
9498
pub fn from<const BITS: usize, const LIMBS: usize>(
9599
a: Uint<BITS, LIMBS>,
@@ -119,6 +123,7 @@ impl Matrix {
119123
/// Panics if `r1 < r0`.
120124
// OPT: Would this be faster using extended binary gcd?
121125
// See <https://en.algorithmica.org/hpc/algorithms/gcd>
126+
#[inline]
122127
#[must_use]
123128
pub fn from_u64(mut r0: u64, mut r1: u64) -> Self {
124129
debug_assert!(r0 >= r1);
@@ -162,6 +167,7 @@ impl Matrix {
162167
///
163168
/// Panics if `a0` does not have the highest bit set.
164169
/// Panics if `a0 < a1`.
170+
#[inline]
165171
#[must_use]
166172
#[allow(clippy::redundant_else)]
167173
#[allow(clippy::cognitive_complexity)] // REFACTOR: Improve
@@ -299,6 +305,7 @@ impl Matrix {
299305
/// simplest out of these solutions.
300306
// OPT: We can update r0 and r1 in place. This won't remove the partially
301307
// redundant call to lehmer_update, but it reduces memory usage.
308+
#[inline]
302309
#[must_use]
303310
pub fn from_u128_prefix(r0: u128, r1: u128) -> Self {
304311
debug_assert!(r0 >= r1);

src/algorithms/gcd/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use core::mem::swap;
1212
/// **Warning.** This struct is not part of the stable API.
1313
///
1414
/// See [`gcd_extended`] for documentation.
15+
#[inline]
1516
#[must_use]
1617
pub fn gcd<const BITS: usize, const LIMBS: usize>(
1718
mut a: Uint<BITS, LIMBS>,
@@ -58,6 +59,7 @@ pub fn gcd<const BITS: usize, const LIMBS: usize>(
5859
///
5960
/// See also `mpn_gcdext_lehmer_n` in GMP.
6061
/// <https://gmplib.org/repo/gmp-6.1/file/tip/mpn/generic/gcdext_lehmer.c#l146>
62+
#[inline]
6163
#[must_use]
6264
pub fn gcd_extended<const BITS: usize, const LIMBS: usize>(
6365
mut a: Uint<BITS, LIMBS>,
@@ -136,6 +138,7 @@ pub fn gcd_extended<const BITS: usize, const LIMBS: usize>(
136138
/// It differs from `gcd_extended` in that it only computes the required
137139
/// cofactor, and returns `None` if the GCD is not one (i.e. when `num` does
138140
/// not have an inverse).
141+
#[inline]
139142
#[must_use]
140143
pub fn inv_mod<const BITS: usize, const LIMBS: usize>(
141144
num: Uint<BITS, LIMBS>,

src/algorithms/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
//! **Warning.** Most functions in this module are currently not considered part
44
//! of the stable API and may be changed or removed in future minor releases.
55
6+
#![allow(missing_docs)] // TODO: document algorithms
7+
68
mod add;
79
pub mod div;
810
mod gcd;

src/algorithms/mul.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use crate::algorithms::{ops::sbb, DoubleWord};
44

5+
#[inline]
56
#[allow(clippy::cast_possible_truncation)] // Intentional truncation.
67
#[allow(dead_code)] // Used for testing
78
pub fn addmul_ref(result: &mut [u64], a: &[u64], b: &[u64]) -> bool {
@@ -225,6 +226,7 @@ fn mac(lhs: &mut u64, a: u64, b: u64, c: u64) -> u64 {
225226
}
226227

227228
/// Computes `lhs *= a` and returns the carry.
229+
#[inline]
228230
pub fn mul_nx1(lhs: &mut [u64], a: u64) -> u64 {
229231
let mut carry = 0;
230232
for lhs in &mut *lhs {

src/algorithms/mul_redc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use core::iter::zip;
33

44
/// See Handbook of Applied Cryptography, Algorithm 14.32, p. 601.
55
#[allow(clippy::cognitive_complexity)] // REFACTOR: Improve
6+
#[inline]
67
pub fn mul_redc(a: &[u64], b: &[u64], result: &mut [u64], m: &[u64], inv: u64) {
78
debug_assert!(!m.is_empty());
89
debug_assert_eq!(a.len(), m.len());

src/base_convert.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ pub enum BaseConvertError {
2323
impl std::error::Error for BaseConvertError {}
2424

2525
impl fmt::Display for BaseConvertError {
26+
#[inline]
2627
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2728
match self {
28-
Self::Overflow => write!(f, "The value is too large to fit the target type"),
29+
Self::Overflow => f.write_str("The value is too large to fit the target type"),
2930
Self::InvalidBase(base) => {
3031
write!(f, "The requested number base {base} is less than two")
3132
}
@@ -156,6 +157,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
156157
/// * [`BaseConvertError::InvalidDigit`] if a digit is out of range.
157158
/// * [`BaseConvertError::Overflow`] if the number is too large to
158159
/// fit.
160+
#[inline]
159161
pub fn from_base_be<I: IntoIterator<Item = u64>>(
160162
base: u64,
161163
digits: I,

src/bit_arr.rs

+6
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,21 @@ impl<const BITS: usize, const LIMBS: usize> Bits<BITS, LIMBS> {
5252
/// types.
5353
pub const ZERO: Self = Self(Uint::<BITS, LIMBS>::ZERO);
5454

55+
/// Returns the inner [Uint].
5556
#[must_use]
5657
#[inline(always)]
5758
pub const fn into_inner(self) -> Uint<BITS, LIMBS> {
5859
self.0
5960
}
6061

62+
/// Returns a reference to the inner [Uint].
6163
#[must_use]
6264
#[inline(always)]
6365
pub const fn as_uint(&self) -> &Uint<BITS, LIMBS> {
6466
&self.0
6567
}
6668

69+
/// Returns a mutable reference to the inner [Uint].
6770
#[must_use]
6871
#[inline(always)]
6972
pub fn as_uint_mut(&mut self) -> &mut Uint<BITS, LIMBS> {
@@ -259,6 +262,7 @@ impl<const BITS: usize, const LIMBS: usize> Bits<BITS, LIMBS> {
259262
impl<const BITS: usize, const LIMBS: usize> Index<usize> for Bits<BITS, LIMBS> {
260263
type Output = bool;
261264

265+
#[inline]
262266
fn index(&self, index: usize) -> &Self::Output {
263267
if self.0.bit(index) {
264268
&true
@@ -271,6 +275,7 @@ impl<const BITS: usize, const LIMBS: usize> Index<usize> for Bits<BITS, LIMBS> {
271275
impl<const BITS: usize, const LIMBS: usize> Not for Bits<BITS, LIMBS> {
272276
type Output = Self;
273277

278+
#[inline]
274279
fn not(self) -> Self {
275280
self.0.not().into()
276281
}
@@ -279,6 +284,7 @@ impl<const BITS: usize, const LIMBS: usize> Not for Bits<BITS, LIMBS> {
279284
impl<const BITS: usize, const LIMBS: usize> Not for &Bits<BITS, LIMBS> {
280285
type Output = Bits<BITS, LIMBS>;
281286

287+
#[inline]
282288
fn not(self) -> Bits<BITS, LIMBS> {
283289
self.0.not().into()
284290
}

src/bits.rs

+16
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
8383
/// Reverses the order of bits in the integer. The least significant bit
8484
/// becomes the most significant bit, second least-significant bit becomes
8585
/// second most-significant bit, etc.
86+
#[inline]
8687
#[must_use]
8788
pub fn reverse_bits(mut self) -> Self {
8889
self.limbs.reverse();
@@ -97,6 +98,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
9798

9899
/// Returns the number of leading zeros in the binary representation of
99100
/// `self`.
101+
#[inline]
100102
#[must_use]
101103
pub fn leading_zeros(&self) -> usize {
102104
self.as_limbs()
@@ -113,13 +115,15 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
113115

114116
/// Returns the number of leading ones in the binary representation of
115117
/// `self`.
118+
#[inline]
116119
#[must_use]
117120
pub fn leading_ones(&self) -> usize {
118121
(self.not()).leading_zeros()
119122
}
120123

121124
/// Returns the number of trailing zeros in the binary representation of
122125
/// `self`.
126+
#[inline]
123127
#[must_use]
124128
pub fn trailing_zeros(&self) -> usize {
125129
self.as_limbs()
@@ -132,6 +136,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
132136

133137
/// Returns the number of trailing ones in the binary representation of
134138
/// `self`.
139+
#[inline]
135140
#[must_use]
136141
pub fn trailing_ones(&self) -> usize {
137142
self.as_limbs()
@@ -143,6 +148,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
143148
}
144149

145150
/// Returns the number of ones in the binary representation of `self`.
151+
#[inline]
146152
#[must_use]
147153
pub fn count_ones(&self) -> usize {
148154
self.as_limbs()
@@ -183,6 +189,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
183189
///
184190
/// If `self` is $<≥> 2^{63}$, then `exponent` will be zero and `bits` will
185191
/// have leading zeros.
192+
#[inline]
186193
#[must_use]
187194
pub fn most_significant_bits(&self) -> (u64, usize) {
188195
let first_set_limb = self
@@ -245,6 +252,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
245252
///
246253
/// Note: This differs from [`u64::overflowing_shl`] which returns `true` if
247254
/// the shift is larger than `BITS` (which is IMHO not very useful).
255+
#[inline]
248256
#[must_use]
249257
pub fn overflowing_shl(mut self, rhs: usize) -> (Self, bool) {
250258
let (limbs, bits) = (rhs / 64, rhs % 64);
@@ -341,6 +349,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
341349
///
342350
/// Note: This differs from [`u64::overflowing_shr`] which returns `true` if
343351
/// the shift is larger than `BITS` (which is IMHO not very useful).
352+
#[inline]
344353
#[must_use]
345354
pub fn overflowing_shr(mut self, rhs: usize) -> (Self, bool) {
346355
let (limbs, bits) = (rhs / 64, rhs % 64);
@@ -399,6 +408,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
399408
}
400409

401410
/// Arithmetic shift right by `rhs` bits.
411+
#[inline]
402412
#[must_use]
403413
pub fn arithmetic_shr(self, rhs: usize) -> Self {
404414
if BITS == 0 {
@@ -414,6 +424,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
414424

415425
/// Shifts the bits to the left by a specified amount, `rhs`, wrapping the
416426
/// truncated bits to the end of the resulting integer.
427+
#[inline]
417428
#[must_use]
418429
#[allow(clippy::missing_const_for_fn)] // False positive
419430
pub fn rotate_left(self, rhs: usize) -> Self {
@@ -424,6 +435,8 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
424435
self << rhs | self >> (BITS - rhs)
425436
}
426437

438+
/// Shifts the bits to the right by a specified amount, `rhs`, wrapping the
439+
/// truncated bits to the beginning of the resulting integer.
427440
#[inline(always)]
428441
#[must_use]
429442
pub fn rotate_right(self, rhs: usize) -> Self {
@@ -438,6 +451,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
438451
impl<const BITS: usize, const LIMBS: usize> Not for Uint<BITS, LIMBS> {
439452
type Output = Self;
440453

454+
#[inline]
441455
fn not(mut self) -> Self::Output {
442456
if BITS == 0 {
443457
return Self::ZERO;
@@ -453,6 +467,7 @@ impl<const BITS: usize, const LIMBS: usize> Not for Uint<BITS, LIMBS> {
453467
impl<const BITS: usize, const LIMBS: usize> Not for &Uint<BITS, LIMBS> {
454468
type Output = Uint<BITS, LIMBS>;
455469

470+
#[inline]
456471
fn not(self) -> Self::Output {
457472
(*self).not()
458473
}
@@ -471,6 +486,7 @@ macro_rules! impl_bit_op {
471486
impl<const BITS: usize, const LIMBS: usize> $trait_assign<&Uint<BITS, LIMBS>>
472487
for Uint<BITS, LIMBS>
473488
{
489+
#[inline]
474490
fn $fn_assign(&mut self, rhs: &Uint<BITS, LIMBS>) {
475491
for (limb, &rhs) in self.limbs.iter_mut().zip(rhs.as_limbs()) {
476492
u64::$fn_assign(limb, rhs);

src/bytes.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
8989
/// Rust issue [#60551].
9090
///
9191
/// [#60551]: https://github.com/rust-lang/rust/issues/60551
92+
#[inline]
9293
#[must_use]
9394
pub fn to_le_bytes<const BYTES: usize>(&self) -> [u8; BYTES] {
9495
// TODO: Use a `const {}` block for this assertion
@@ -280,8 +281,8 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
280281
/// This needs to be public because it is used in the `Uint` type,
281282
/// specifically in the [`to_be_bytes()`][Uint::to_be_bytes] and related
282283
/// functions.
283-
#[must_use]
284284
#[inline]
285+
#[must_use]
285286
pub const fn nbytes(bits: usize) -> usize {
286287
(bits + 7) / 8
287288
}

0 commit comments

Comments
 (0)