Skip to content

Commit

Permalink
Add const fn constructors for NonZero<Uint> and NonZero<Limb> (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri authored Aug 28, 2023
1 parent 177035e commit 3126405
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/non_zero.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Wrapper type for non-zero integers.
use crate::{Encoding, Integer, Limb, Uint, Zero};
use crate::{CtChoice, Encoding, Integer, Limb, Uint, Zero};
use core::{
fmt,
num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8},
Expand All @@ -24,6 +24,22 @@ use serdect::serde::{
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
pub struct NonZero<T: Zero>(T);

impl NonZero<Limb> {
/// Creates a new non-zero limb in a const context.
/// The second return value is `FALSE` if `n` is zero, `TRUE` otherwise.
pub const fn const_new(n: Limb) -> (Self, CtChoice) {
(Self(n), n.ct_is_nonzero())
}
}

impl<const LIMBS: usize> NonZero<Uint<LIMBS>> {
/// Creates a new non-zero integer in a const context.
/// The second return value is `FALSE` if `n` is zero, `TRUE` otherwise.
pub const fn const_new(n: Uint<LIMBS>) -> (Self, CtChoice) {
(Self(n), n.ct_is_nonzero())
}
}

impl<T> NonZero<T>
where
T: Zero,
Expand Down

0 comments on commit 3126405

Please sign in to comment.