rust-lang / rust-lang/rust

Missing TryFrom<iX> implementation for NonZero<iY>

Open
#162,538 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs-triage T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Currently, we have the following implementations:

  • TryFrom<iX>for NonZero<iX> (for i in [i, u] and X in [8, 16, 32, 64, 128, size])
  • TryFrom<NonZero<iX>> for NonZero<jY> (for i, j in [i, u]^2 and X, Y in [8, 16, 32, 64, 128, size]^2 if i != j or X > Y)

However, we do not have

  • TryFrom<iX> for NonZero<jY> (for i, j in [i, u]^2, X, Y in [8, 16, 32, 64, 128]^2 if i != j or X > Y)

This felt like an oversight to me. I found https://github.com/rust-lang/rust/pull/72712 where it looks like a decision on whether or not those methods should exist was put off. I know I can chain two TryFrom impls together but that feels very unergonomic to me.

In case it matters, my use case is that I get a list of core::time::Duration that I need to store in a list of Option<NonZeroU32> with microsecond precision, but as_micros() returns a u128, so I need to convert it (or return an error if that is not possible).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining the existing TryFrom implementations for NonZero integer types and compare their supported signedness and widths with the combinations listed in this issue. Determine whether the missing direct conversions fit the prior discussion in PR 72712, then add coverage for the intended combinations and verify that overflow and zero cases remain handled correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.