Skip to content

Commit

Permalink
NFC: Polyfill const_option feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Sep 30, 2023
1 parent 165e8a7 commit f15eec9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
4 changes: 3 additions & 1 deletion src/polyfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ mod leading_zeros_skipped;
#[cfg(test)]
mod test;

pub use self::{array_flat_map::ArrayFlatMap, chunks_fixed::*};
mod unwrap_const;

pub use self::{array_flat_map::ArrayFlatMap, chunks_fixed::*, unwrap_const::unwrap_const};

#[cfg(feature = "alloc")]
pub use leading_zeros_skipped::LeadingZerosStripped;
15 changes: 15 additions & 0 deletions src/polyfill/unwrap_const.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// Polyfill for `Option::unwrap()` as a const fn; feature `const_option`.
/// https://github.com/rust-lang/rust/issues/67441.
/// TODO(MSRV): Replace this with `x.unwrap()`.
///
/// `T: Copy` avoids "constant functions cannot evaluate destructors."
pub const fn unwrap_const<T>(x: Option<T>) -> T
where
T: Copy,
{
if let Some(x) = x {
x

Check warning on line 11 in src/polyfill/unwrap_const.rs

View check run for this annotation

Codecov / codecov/patch

src/polyfill/unwrap_const.rs#L6-L11

Added lines #L6 - L11 were not covered by tests
} else {
panic!("unwrap_const on `None`");

Check warning on line 13 in src/polyfill/unwrap_const.rs

View check run for this annotation

Codecov / codecov/patch

src/polyfill/unwrap_const.rs#L13

Added line #L13 was not covered by tests
}
}

Check warning on line 15 in src/polyfill/unwrap_const.rs

View check run for this annotation

Codecov / codecov/patch

src/polyfill/unwrap_const.rs#L15

Added line #L15 was not covered by tests
22 changes: 4 additions & 18 deletions src/rsa/public_exponent.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error;
use crate::polyfill::{ArrayFlatMap, LeadingZerosStripped};
use crate::polyfill::{unwrap_const, ArrayFlatMap, LeadingZerosStripped};
use core::num::NonZeroU64;

/// The exponent `e` of an RSA public key.
Expand All @@ -10,16 +10,8 @@ impl PublicExponent {
#[cfg(test)]
const ALL_CONSTANTS: [Self; 3] = [Self::_3, Self::_65537, Self::MAX];

// TODO: Use `NonZeroU64::new(...).unwrap()` when `feature(const_panic)` is
// stable.
pub(super) const _3: Self = Self(match NonZeroU64::new(3) {
Some(nz) => nz,
None => unreachable!(),
});
pub(super) const _65537: Self = Self(match NonZeroU64::new(65537) {
Some(nz) => nz,
None => unreachable!(),
});
pub(super) const _3: Self = Self(unwrap_const(NonZeroU64::new(3)));
pub(super) const _65537: Self = Self(unwrap_const(NonZeroU64::new(65537)));

// This limit was chosen to bound the performance of the simple
// exponentiation-by-squaring implementation in `elem_exp_vartime`. In
Expand All @@ -32,13 +24,7 @@ impl PublicExponent {
// [1] https://www.imperialviolet.org/2012/03/16/rsae.html
// [2] https://www.imperialviolet.org/2012/03/17/rsados.html
// [3] https://msdn.microsoft.com/en-us/library/aa387685(VS.85).aspx
//
// TODO: Use `NonZeroU64::new(...).unwrap()` when `feature(const_panic)` is
// stable.
const MAX: Self = Self(match NonZeroU64::new((1u64 << 33) - 1) {
Some(nz) => nz,
None => unreachable!(),
});
const MAX: Self = Self(unwrap_const(NonZeroU64::new((1u64 << 33) - 1)));

pub(super) fn from_be_bytes(
input: untrusted::Input,
Expand Down

0 comments on commit f15eec9

Please sign in to comment.