From 857d11b522ca9dd981eb55f989fd65d043a5ae1d Mon Sep 17 00:00:00 2001 From: David Palm Date: Mon, 3 Jun 2024 15:45:45 +0200 Subject: [PATCH 1/4] Add a new_unchecked const method to NonZero Makes it so users can have `NonZero` constants. --- src/non_zero.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/non_zero.rs b/src/non_zero.rs index 7ee402111..cd77a5eac 100644 --- a/src/non_zero.rs +++ b/src/non_zero.rs @@ -35,6 +35,11 @@ impl NonZero { CtOption::new(Self(n), !is_zero) } + /// Create a new non-zero integer without zero-checks. + pub const fn new_unchecked(n: T) -> Self { + Self(n) + } + /// Provides access to the contents of `NonZero` in a `const` context. pub const fn as_ref(&self) -> &T { &self.0 From cf6486715602000b50de25e3a99e696dc7496194 Mon Sep 17 00:00:00 2001 From: David Palm Date: Tue, 4 Jun 2024 12:39:02 +0200 Subject: [PATCH 2/4] Adds const_new to NonZero> --- src/non_zero.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/non_zero.rs b/src/non_zero.rs index cd77a5eac..75ec24438 100644 --- a/src/non_zero.rs +++ b/src/non_zero.rs @@ -35,11 +35,6 @@ impl NonZero { CtOption::new(Self(n), !is_zero) } - /// Create a new non-zero integer without zero-checks. - pub const fn new_unchecked(n: T) -> Self { - Self(n) - } - /// Provides access to the contents of `NonZero` in a `const` context. pub const fn as_ref(&self) -> &T { &self.0 @@ -116,6 +111,18 @@ impl NonZero { } impl NonZero> { + /// Creates a new non-zero integer in a const context. + /// # Panics + /// + /// Panics if the value is zero. + pub const fn const_new(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 { From 7f6bbb12fcef27464186b57cd08fd19358af36c8 Mon Sep 17 00:00:00 2001 From: David Palm Date: Fri, 26 Jul 2024 10:57:39 +0200 Subject: [PATCH 3/4] Rename to `new_unwrap` and tweak docs --- src/non_zero.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/non_zero.rs b/src/non_zero.rs index 75ec24438..9484127d4 100644 --- a/src/non_zero.rs +++ b/src/non_zero.rs @@ -112,10 +112,12 @@ impl NonZero { impl NonZero> { /// Creates a new non-zero integer in a const context. - /// # Panics - /// /// Panics if the value is zero. - pub const fn const_new(n: Uint) -> Self { + /// + /// In future versions of Rust it should be possible to replace this with + /// `NonZero::new(…).unwrap()` + // TODO: Remove when `Result::unwrap` is const (https://github.com/rust-lang/rust/issues/82814) + pub const fn new_unwrap(n: Uint) -> Self { if n.is_nonzero().is_true_vartime() { Self(n) } else { From e22a2c62eab0683dcc30dbddb92cdcc5d8e17ef2 Mon Sep 17 00:00:00 2001 From: David Date: Fri, 26 Jul 2024 17:42:41 +0200 Subject: [PATCH 4/4] Update src/non_zero.rs Co-authored-by: Tony Arcieri --- src/non_zero.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/non_zero.rs b/src/non_zero.rs index 9484127d4..fe91b1ee7 100644 --- a/src/non_zero.rs +++ b/src/non_zero.rs @@ -116,7 +116,7 @@ impl NonZero> { /// /// In future versions of Rust it should be possible to replace this with /// `NonZero::new(…).unwrap()` - // TODO: Remove when `Result::unwrap` is const (https://github.com/rust-lang/rust/issues/82814) + // 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)