Skip to content

Commit 1ef8172

Browse files
authored
NonZero: remove from_uint; reorganize module (#486)
The `NonZero::from_uint` method duplicates the `Uint::to_nz` method, and isn't actually used anywhere in this codebase. Also reorganizes the module to place all the inherent impls at the top, and the trait impls at the bottom.
1 parent 296bd55 commit 1ef8172

File tree

1 file changed

+104
-118
lines changed

1 file changed

+104
-118
lines changed

src/non_zero.rs

Lines changed: 104 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,65 @@ where
8383
}
8484
}
8585

86+
impl NonZero<Limb> {
87+
/// Create a [`NonZero<Limb>`] from a [`NonZeroU8`] (const-friendly)
88+
// TODO(tarcieri): replace with `const impl From<NonZeroU8>` when stable
89+
pub const fn from_u8(n: NonZeroU8) -> Self {
90+
Self(Limb::from_u8(n.get()))
91+
}
92+
93+
/// Create a [`NonZero<Limb>`] from a [`NonZeroU16`] (const-friendly)
94+
// TODO(tarcieri): replace with `const impl From<NonZeroU16>` when stable
95+
pub const fn from_u16(n: NonZeroU16) -> Self {
96+
Self(Limb::from_u16(n.get()))
97+
}
98+
99+
/// Create a [`NonZero<Limb>`] from a [`NonZeroU32`] (const-friendly)
100+
// TODO(tarcieri): replace with `const impl From<NonZeroU32>` when stable
101+
pub const fn from_u32(n: NonZeroU32) -> Self {
102+
Self(Limb::from_u32(n.get()))
103+
}
104+
105+
/// Create a [`NonZero<Limb>`] from a [`NonZeroU64`] (const-friendly)
106+
// TODO(tarcieri): replace with `const impl From<NonZeroU64>` when stable
107+
#[cfg(target_pointer_width = "64")]
108+
pub const fn from_u64(n: NonZeroU64) -> Self {
109+
Self(Limb::from_u64(n.get()))
110+
}
111+
}
112+
113+
impl<const LIMBS: usize> NonZero<Uint<LIMBS>> {
114+
/// Create a [`NonZero<Uint>`] from a [`NonZeroU8`] (const-friendly)
115+
// TODO(tarcieri): replace with `const impl From<NonZeroU8>` when stable
116+
pub const fn from_u8(n: NonZeroU8) -> Self {
117+
Self(Uint::from_u8(n.get()))
118+
}
119+
120+
/// Create a [`NonZero<Uint>`] from a [`NonZeroU16`] (const-friendly)
121+
// TODO(tarcieri): replace with `const impl From<NonZeroU16>` when stable
122+
pub const fn from_u16(n: NonZeroU16) -> Self {
123+
Self(Uint::from_u16(n.get()))
124+
}
125+
126+
/// Create a [`NonZero<Uint>`] from a [`NonZeroU32`] (const-friendly)
127+
// TODO(tarcieri): replace with `const impl From<NonZeroU32>` when stable
128+
pub const fn from_u32(n: NonZeroU32) -> Self {
129+
Self(Uint::from_u32(n.get()))
130+
}
131+
132+
/// Create a [`NonZero<Uint>`] from a [`NonZeroU64`] (const-friendly)
133+
// TODO(tarcieri): replace with `const impl From<NonZeroU64>` when stable
134+
pub const fn from_u64(n: NonZeroU64) -> Self {
135+
Self(Uint::from_u64(n.get()))
136+
}
137+
138+
/// Create a [`NonZero<Uint>`] from a [`NonZeroU128`] (const-friendly)
139+
// TODO(tarcieri): replace with `const impl From<NonZeroU128>` when stable
140+
pub const fn from_u128(n: NonZeroU128) -> Self {
141+
Self(Uint::from_u128(n.get()))
142+
}
143+
}
144+
86145
#[cfg(feature = "generic-array")]
87146
impl<T> NonZero<T>
88147
where
@@ -149,78 +208,6 @@ where
149208
}
150209
}
151210

152-
impl<T> fmt::Display for NonZero<T>
153-
where
154-
T: fmt::Display,
155-
{
156-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157-
fmt::Display::fmt(&self.0, f)
158-
}
159-
}
160-
161-
impl<T> fmt::Binary for NonZero<T>
162-
where
163-
T: fmt::Binary,
164-
{
165-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
166-
fmt::Binary::fmt(&self.0, f)
167-
}
168-
}
169-
170-
impl<T> fmt::Octal for NonZero<T>
171-
where
172-
T: fmt::Octal,
173-
{
174-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175-
fmt::Octal::fmt(&self.0, f)
176-
}
177-
}
178-
179-
impl<T> fmt::LowerHex for NonZero<T>
180-
where
181-
T: fmt::LowerHex,
182-
{
183-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
184-
fmt::LowerHex::fmt(&self.0, f)
185-
}
186-
}
187-
188-
impl<T> fmt::UpperHex for NonZero<T>
189-
where
190-
T: fmt::UpperHex,
191-
{
192-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
193-
fmt::UpperHex::fmt(&self.0, f)
194-
}
195-
}
196-
197-
impl NonZero<Limb> {
198-
/// Create a [`NonZero<Limb>`] from a [`NonZeroU8`] (const-friendly)
199-
// TODO(tarcieri): replace with `const impl From<NonZeroU8>` when stable
200-
pub const fn from_u8(n: NonZeroU8) -> Self {
201-
Self(Limb::from_u8(n.get()))
202-
}
203-
204-
/// Create a [`NonZero<Limb>`] from a [`NonZeroU16`] (const-friendly)
205-
// TODO(tarcieri): replace with `const impl From<NonZeroU16>` when stable
206-
pub const fn from_u16(n: NonZeroU16) -> Self {
207-
Self(Limb::from_u16(n.get()))
208-
}
209-
210-
/// Create a [`NonZero<Limb>`] from a [`NonZeroU32`] (const-friendly)
211-
// TODO(tarcieri): replace with `const impl From<NonZeroU32>` when stable
212-
pub const fn from_u32(n: NonZeroU32) -> Self {
213-
Self(Limb::from_u32(n.get()))
214-
}
215-
216-
/// Create a [`NonZero<Limb>`] from a [`NonZeroU64`] (const-friendly)
217-
// TODO(tarcieri): replace with `const impl From<NonZeroU64>` when stable
218-
#[cfg(target_pointer_width = "64")]
219-
pub const fn from_u64(n: NonZeroU64) -> Self {
220-
Self(Limb::from_u64(n.get()))
221-
}
222-
}
223-
224211
impl From<NonZeroU8> for NonZero<Limb> {
225212
fn from(integer: NonZeroU8) -> Self {
226213
Self::from_u8(integer)
@@ -246,52 +233,6 @@ impl From<NonZeroU64> for NonZero<Limb> {
246233
}
247234
}
248235

249-
impl<const LIMBS: usize> NonZero<Uint<LIMBS>> {
250-
/// Create a [`NonZero<Uint>`] from a [`Uint`] (const-friendly)
251-
pub const fn from_uint(n: Uint<LIMBS>) -> Self {
252-
let mut i = 0;
253-
let mut found_non_zero = false;
254-
while i < LIMBS {
255-
if n.as_limbs()[i].0 != 0 {
256-
found_non_zero = true;
257-
}
258-
i += 1;
259-
}
260-
assert!(found_non_zero, "found zero");
261-
Self(n)
262-
}
263-
264-
/// Create a [`NonZero<Uint>`] from a [`NonZeroU8`] (const-friendly)
265-
// TODO(tarcieri): replace with `const impl From<NonZeroU8>` when stable
266-
pub const fn from_u8(n: NonZeroU8) -> Self {
267-
Self(Uint::from_u8(n.get()))
268-
}
269-
270-
/// Create a [`NonZero<Uint>`] from a [`NonZeroU16`] (const-friendly)
271-
// TODO(tarcieri): replace with `const impl From<NonZeroU16>` when stable
272-
pub const fn from_u16(n: NonZeroU16) -> Self {
273-
Self(Uint::from_u16(n.get()))
274-
}
275-
276-
/// Create a [`NonZero<Uint>`] from a [`NonZeroU32`] (const-friendly)
277-
// TODO(tarcieri): replace with `const impl From<NonZeroU32>` when stable
278-
pub const fn from_u32(n: NonZeroU32) -> Self {
279-
Self(Uint::from_u32(n.get()))
280-
}
281-
282-
/// Create a [`NonZero<Uint>`] from a [`NonZeroU64`] (const-friendly)
283-
// TODO(tarcieri): replace with `const impl From<NonZeroU64>` when stable
284-
pub const fn from_u64(n: NonZeroU64) -> Self {
285-
Self(Uint::from_u64(n.get()))
286-
}
287-
288-
/// Create a [`NonZero<Uint>`] from a [`NonZeroU128`] (const-friendly)
289-
// TODO(tarcieri): replace with `const impl From<NonZeroU128>` when stable
290-
pub const fn from_u128(n: NonZeroU128) -> Self {
291-
Self(Uint::from_u128(n.get()))
292-
}
293-
}
294-
295236
impl<const LIMBS: usize> From<NonZeroU8> for NonZero<Uint<LIMBS>> {
296237
fn from(integer: NonZeroU8) -> Self {
297238
Self::from_u8(integer)
@@ -322,6 +263,51 @@ impl<const LIMBS: usize> From<NonZeroU128> for NonZero<Uint<LIMBS>> {
322263
}
323264
}
324265

266+
impl<T> fmt::Display for NonZero<T>
267+
where
268+
T: fmt::Display,
269+
{
270+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
271+
fmt::Display::fmt(&self.0, f)
272+
}
273+
}
274+
275+
impl<T> fmt::Binary for NonZero<T>
276+
where
277+
T: fmt::Binary,
278+
{
279+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
280+
fmt::Binary::fmt(&self.0, f)
281+
}
282+
}
283+
284+
impl<T> fmt::Octal for NonZero<T>
285+
where
286+
T: fmt::Octal,
287+
{
288+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
289+
fmt::Octal::fmt(&self.0, f)
290+
}
291+
}
292+
293+
impl<T> fmt::LowerHex for NonZero<T>
294+
where
295+
T: fmt::LowerHex,
296+
{
297+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
298+
fmt::LowerHex::fmt(&self.0, f)
299+
}
300+
}
301+
302+
impl<T> fmt::UpperHex for NonZero<T>
303+
where
304+
T: fmt::UpperHex,
305+
{
306+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
307+
fmt::UpperHex::fmt(&self.0, f)
308+
}
309+
}
310+
325311
#[cfg(feature = "serde")]
326312
impl<'de, T: Deserialize<'de> + Zero> Deserialize<'de> for NonZero<T> {
327313
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>

0 commit comments

Comments
 (0)