From 2fe368ccb0dade0b969af0acfae66dbbcc0136d8 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sun, 4 Jan 2026 09:29:59 -0700 Subject: [PATCH] Add `get_copy` method to `NonZero`/`Odd` By adding a `T: Copy` bound, we can have a function equivalent to `get` which is `const fn`, since `Copy` types are `!Drop` and otherwise `get` needs to worry about destructors --- src/non_zero.rs | 12 ++++++++++++ src/odd.rs | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/non_zero.rs b/src/non_zero.rs index c913d5d0e..ca1b74c46 100644 --- a/src/non_zero.rs +++ b/src/non_zero.rs @@ -61,6 +61,18 @@ impl NonZero { pub fn get(self) -> T { self.0 } + + /// Returns a copy of the inner value for `Copy` types. + /// + /// This allows the function to be `const fn`, since `Copy` is implicitly `!Drop`, which avoids + /// problems around `const fn` destructors. + #[inline] + pub const fn get_copy(self) -> T + where + T: Copy, + { + self.0 + } } impl NonZero { diff --git a/src/odd.rs b/src/odd.rs index f70f56f57..bb159b98a 100644 --- a/src/odd.rs +++ b/src/odd.rs @@ -61,6 +61,18 @@ impl Odd { pub fn get(self) -> T { self.0 } + + /// Returns a copy of the inner value for `Copy` types. + /// + /// This allows the function to be `const fn`, since `Copy` is implicitly `!Drop`, which avoids + /// problems around `const fn` destructors. + #[inline] + pub const fn get_copy(self) -> T + where + T: Copy, + { + self.0 + } } impl Odd {