rust-lang / libs-team

The home of the library team
Apache License 2.0
128 stars 19 forks source link

Add `LowerExp` and `UpperExp` implementations to `NonZero` #458

Closed rick-de-water closed 1 month ago

rick-de-water commented 1 month ago

Proposal

Problem statement

LowerExp and UpperExp format traits implementations are missing from the NonZero integer types. All other (Binary, *Hex, Octal, etc) have been implemented, so it looks like an oversight, especially since the integer primitives themselves do implement LowerExp and UpperExp.

Motivating examples or use cases

UpperExp example changed to use NonZeroU32

fn main() {
    let x = core::num::NonZeroU32::new(42).unwrap(); // 42 is '4.2E1' in scientific notation

    assert_eq!(format!("{x:E}"), "4.2E1");
}

Solution sketch

Implement LowerExp and UpperExp. It probably only involves adding impl_nonzero_fmt!(LowerExp) and impl_nonzero_fmt!(UpperExp) to the list of format impls

Alternatives

Use .get() like a pleb, which then needs to be passed as an argument since you cannot call functions from format! and friends.

Links and related work

N/A

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

Second, if there's a concrete solution:

dtolnay commented 1 month ago

Please send a PR. I agree this looks like an oversight from https://github.com/rust-lang/rust/pull/48265.