diff --git a/src/non_zero.rs b/src/non_zero.rs index 7ee402111..fe91b1ee7 100644 --- a/src/non_zero.rs +++ b/src/non_zero.rs @@ -111,6 +111,20 @@ impl NonZero { } impl NonZero> { + /// Creates a new non-zero integer in a const context. + /// Panics if the value is zero. + /// + /// In future versions of Rust it should be possible to replace this with + /// `NonZero::new(…).unwrap()` + // TODO: Remove when `Self::new` and `CtOption::unwrap` support `const fn` + pub const fn new_unwrap(n: Uint) -> Self { + if n.is_nonzero().is_true_vartime() { + Self(n) + } else { + panic!("Invalid value: zero") + } + } + /// Create a [`NonZero`] from a [`NonZeroU8`] (const-friendly) // TODO(tarcieri): replace with `const impl From` when stable pub const fn from_u8(n: NonZeroU8) -> Self {