Skip to content

Commit 8ff1ce3

Browse files
committed
Merge #282: Fix pedantic clippy lints
5b262ee Fix pedantic clippy lints in fieldvec (Abeeujah) 510b0c4 Refactor primitives::encode Option handling (Abeeujah) 5f2a228 Refactor witness_version to use functional combinators (Abeeujah) dc217db Fix trailing whitespace in ErrorIterator error message (Abeeujah) f2ace2c Fix pedantic lints in primitives::checksum module (Abeeujah) Pull request description: This PR refactors a couple of data flow control and constructs regarding the `Option<T>` type, to be as idiomatic and ergonomic as the MSRV permits. Commits are by Modules, but can be squashed if needed. ACKs for top commit: apoelstra: ACK 5b262ee; successfully ran local tests Tree-SHA512: 842a6e50236f7290f5b1e2b96cedd2136a1be6344fb3ff7bb627f5f6e79bbe75baf686c310bb82b4c55d9660d0297cd8f651dc46909bc21098f8a856895c1988
2 parents 134c6f1 + 5b262ee commit 8ff1ce3

5 files changed

Lines changed: 39 additions & 69 deletions

File tree

src/primitives/checksum.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use core::marker::PhantomData;
88
use core::{fmt, mem, ops};
99

1010
use super::Polynomial;
11-
use crate::primitives::hrp::Hrp;
11+
use crate::primitives::hrp::{Hrp, LowercaseByteIter};
1212
use crate::{Fe1024, Fe32};
1313

1414
/// Trait defining a particular checksum.
@@ -179,7 +179,7 @@ impl<'a, ExtField> PrintImpl<'a, ExtField> {
179179
/// Panics if any of the input values fail various sanity checks.
180180
pub fn new(name: &'a str, generator: &'a [Fe32], target: &'a [Fe32]) -> Self {
181181
// Sanity checks.
182-
assert_ne!(name.len(), 0, "type name cannot be the empty string",);
182+
assert_ne!(name.len(), 0, "type name cannot be the empty string");
183183
assert_ne!(
184184
generator.len(),
185185
0,
@@ -396,9 +396,7 @@ impl PackedFe32 for PackedNull {
396396

397397
#[inline]
398398
fn pack<I: Iterator<Item = u8>>(mut iter: I) -> Self {
399-
if iter.next().is_some() {
400-
panic!("Cannot pack anything into a PackedNull");
401-
}
399+
assert!(iter.next().is_none(), "Cannot pack anything into a PackedNull");
402400
Self
403401
}
404402
}
@@ -469,13 +467,12 @@ impl Iterator for HrpFe32Iter<'_> {
469467
#[inline]
470468
fn next(&mut self) -> Option<Fe32> {
471469
if let Some(ref mut high_iter) = &mut self.high_iter {
472-
match high_iter.next() {
473-
Some(high) => return Some(Fe32(high >> 5)),
474-
None => {
475-
self.high_iter = None;
476-
return Some(Fe32::Q);
477-
}
478-
}
470+
return if let Some(high) = high_iter.next() {
471+
Some(Fe32(high >> 5))
472+
} else {
473+
self.high_iter = None;
474+
Some(Fe32::Q)
475+
};
479476
}
480477
if let Some(ref mut low_iter) = &mut self.low_iter {
481478
match low_iter.next() {
@@ -488,17 +485,11 @@ impl Iterator for HrpFe32Iter<'_> {
488485

489486
#[inline]
490487
fn size_hint(&self) -> (usize, Option<usize>) {
491-
let high = match &self.high_iter {
492-
Some(high_iter) => {
493-
let (min, max) = high_iter.size_hint();
494-
(min + 1, max.map(|max| max + 1)) // +1 for the extra Q
495-
}
496-
None => (0, Some(0)),
497-
};
498-
let low = match &self.low_iter {
499-
Some(low_iter) => low_iter.size_hint(),
500-
None => (0, Some(0)),
501-
};
488+
let high = self.high_iter.as_ref().map_or((0, Some(0)), |high_iter| {
489+
let (min, max) = high_iter.size_hint();
490+
(min + 1, max.map(|max| max + 1)) // +1 for the extra Q
491+
});
492+
let low = self.low_iter.as_ref().map_or((0, Some(0)), LowercaseByteIter::size_hint);
502493

503494
let min = high.0 + low.0;
504495
let max = high.1.zip(low.1).map(|(high, low)| high + low);

src/primitives/correction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ impl<Ck: Checksum> Iterator for ErrorIterator<'_, Ck> {
299299
let ret = -num / den;
300300
match ret.try_into() {
301301
Ok(ret) => Some((neg_i, ret)),
302-
Err(_) => unreachable!("error guaranteed to lie in base field"),
302+
Err(_) => unreachable!("error guaranteed to lie in base field"),
303303
}
304304
}
305305
}

src/primitives/decode.rs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,10 @@ impl<'s> UncheckedHrpstring<'s> {
215215
/// ```
216216
#[inline]
217217
pub fn witness_version(&self) -> Option<Fe32> {
218-
let data_part = self.data_part_ascii();
219-
if data_part.is_empty() {
220-
return None;
221-
}
222-
223-
// unwrap ok because we know we gave valid bech32 characters.
224-
let witness_version = Fe32::from_char(data_part[0].into()).unwrap();
225-
if witness_version.to_u8() > 16 {
226-
return None;
227-
}
228-
Some(witness_version)
218+
self.data_part_ascii
219+
.first()
220+
.map(|&ch| Fe32::from_char(ch.into()).unwrap()) // unwrap ok because we know we gave valid bech32 characters.
221+
.filter(|version| version.to_u8() <= 16)
229222
}
230223

231224
/// Validates that data has a valid checksum for the `Ck` algorithm and returns a [`CheckedHrpstring`].
@@ -427,17 +420,10 @@ impl<'s> CheckedHrpstring<'s> {
427420
/// ```
428421
#[inline]
429422
pub fn witness_version(&self) -> Option<Fe32> {
430-
let data_part = self.data_part_ascii_no_checksum();
431-
if data_part.is_empty() {
432-
return None;
433-
}
434-
435-
// unwrap ok because we know we gave valid bech32 characters.
436-
let witness_version = Fe32::from_char(data_part[0].into()).unwrap();
437-
if witness_version.to_u8() > 16 {
438-
return None;
439-
}
440-
Some(witness_version)
423+
self.data_part_ascii_no_checksum()
424+
.first()
425+
.map(|&ch| Fe32::from_char(ch.into()).unwrap()) // unwrap ok because we know we gave valid bech32 characters.
426+
.filter(|version| version.to_u8() <= 16)
441427
}
442428

443429
/// Returns an iterator that yields the data part of the parsed bech32 encoded string as [`Fe32`]s.

src/primitives/encode.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -209,40 +209,36 @@ where
209209
#[inline]
210210
fn next(&mut self) -> Option<char> {
211211
if let Some(ref mut hrp_iter) = self.hrp_iter {
212-
match hrp_iter.next() {
213-
Some(c) => return Some(c),
214-
None => {
215-
self.hrp_iter = None;
216-
return Some('1');
217-
}
218-
}
212+
return if let Some(c) = hrp_iter.next() {
213+
Some(c)
214+
} else {
215+
self.hrp_iter = None;
216+
Some('1')
217+
};
219218
}
220219

221-
self.checksummed.next().map(|fe| fe.to_char())
220+
self.checksummed.next().map(Fe32::to_char)
222221
}
223222

224223
#[inline]
225224
fn size_hint(&self) -> (usize, Option<usize>) {
226-
match &self.hrp_iter {
225+
self.hrp_iter.as_ref().map_or_else(
227226
// We have yielded the hrp and separator already.
228-
None => self.checksummed.size_hint(),
227+
|| self.checksummed.size_hint(),
229228
// Yet to finish yielding the hrp (and the separator).
230-
Some(hrp_iter) => {
229+
|hrp_iter| {
231230
let (hrp_min, hrp_max) = hrp_iter.size_hint();
232231
let (chk_min, chk_max) = self.checksummed.size_hint();
233232

234233
let min = hrp_min + 1 + chk_min; // +1 for the separator.
235234

236235
// To provide a max boundary we need to have gotten a value from the hrp iter as well as the
237236
// checksummed iter, otherwise we have to return None since we cannot know the maximum.
238-
let max = match (hrp_max, chk_max) {
239-
(Some(hrp_max), Some(chk_max)) => Some(hrp_max + 1 + chk_max),
240-
(_, _) => None,
241-
};
237+
let max = hrp_max.zip(chk_max).map(|(hrp, chk)| hrp + 1 + chk);
242238

243239
(min, max)
244-
}
245-
}
240+
},
241+
)
246242
}
247243
}
248244

@@ -330,10 +326,7 @@ where
330326

331327
#[inline]
332328
fn size_hint(&self) -> (usize, Option<usize>) {
333-
let hrp = match &self.hrp_iter {
334-
Some(hrp_iter) => hrp_iter.size_hint(),
335-
None => (0, Some(0)),
336-
};
329+
let hrp = self.hrp_iter.as_ref().map_or((0, Some(0)), HrpFe32Iter::size_hint);
337330

338331
let data = self.checksummed.size_hint();
339332

src/primitives/fieldvec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ impl<F: Clone + Default> iter::FromIterator<F> for FieldVec<F> {
352352
// This goofy map construction is needed because we cannot use the
353353
// `[F::default(); N]` syntax without adding a `Copy` bound to `F`.
354354
// After Rust 1.63 we will be able to use array::from_fn.
355-
let mut inner_a = [(); NO_ALLOC_MAX_LENGTH].map(|_| F::default());
355+
let mut inner_a = [(); NO_ALLOC_MAX_LENGTH].map(|()| F::default());
356356
let mut len = 0;
357357
for elem in iter.by_ref().take(NO_ALLOC_MAX_LENGTH) {
358358
inner_a[len] = elem;
@@ -389,7 +389,7 @@ impl<F: Clone + Default> iter::FromIterator<F> for FieldVec<F> {
389389

390390
impl<F: fmt::Display> fmt::Display for FieldVec<F> {
391391
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
392-
for fe in self.iter() {
392+
for fe in self {
393393
fe.fmt(f)?;
394394
}
395395
Ok(())

0 commit comments

Comments
 (0)