Skip to content

Commit 1f86512

Browse files
committed
Enable const_panic feature
Use this to make ValidPrime::new a const function. Marked is_valid_prime as const too.
1 parent f1f6798 commit 1f86512

File tree

2 files changed

+5
-12
lines changed

2 files changed

+5
-12
lines changed

ext/crates/fp/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(const_panic)]
12
#![allow(clippy::many_single_char_names)]
23
#![allow(clippy::unreadable_literal)]
34

ext/crates/fp/src/prime.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ pub struct ValidPrime {
1313
}
1414

1515
impl ValidPrime {
16-
pub fn new(p: u32) -> Self {
17-
assert!(is_valid_prime(p), "Invalid prime: {}", p);
16+
pub const fn new(p: u32) -> Self {
17+
assert!(is_valid_prime(p));
1818

1919
#[cfg(not(feature = "prime-two"))]
2020
{ Self { p } }
@@ -30,14 +30,6 @@ impl ValidPrime {
3030
None
3131
}
3232
}
33-
34-
/// Create a ValidPrime without checking the number is a valid prime. Calling with an invalid
35-
/// value invokes undefined behaviour. The main reason for this is that
36-
/// [ValidPrime::new](ValidPrime::new) is not const due to the checks. If the checked version
37-
/// can be made const, we can remove this function
38-
pub const unsafe fn new_unsafe(p: u32) -> Self {
39-
Self { p }
40-
}
4133
}
4234

4335
impl std::ops::Deref for ValidPrime {
@@ -86,12 +78,12 @@ impl<'de> Deserialize<'de> for ValidPrime {
8678
}
8779

8880
#[cfg(not(feature = "prime-two"))]
89-
pub fn is_valid_prime(p : u32) -> bool {
81+
pub const fn is_valid_prime(p : u32) -> bool {
9082
(p as usize) < MAX_PRIME && PRIME_TO_INDEX_MAP[p as usize] != NOT_A_PRIME
9183
}
9284

9385
#[cfg(feature = "prime-two")]
94-
pub fn is_valid_prime(p : u32) -> bool {
86+
pub const fn is_valid_prime(p : u32) -> bool {
9587
p == 2
9688
}
9789

0 commit comments

Comments
 (0)