rust/library
bors da7ada584a Auto merge of #82703 - iago-lito:nonzero_add_mul_pow, r=m-ou-se
Implement nonzero arithmetics for NonZero types.

Hello'all, this is my first PR to this repo.

Non-zero natural numbers are stable by addition/multiplication/exponentiation, so it makes sense to make this arithmetic possible with `NonZeroU*`.

The major pitfall is that overflowing underlying `u*` types possibly lead to underlying `0` values, which break the major invariant of `NonZeroU*`. To accommodate it, only `checked_` and `saturating_` operations are implemented.

Other variants allowing wrapped results like `wrapping_` or `overflowing_` are ruled out *de facto*.

`impl Add<u*> for NonZeroU* { .. }` was considered, as it panics on overflow which enforces the invariant, but it does not so in release mode. I considered forcing `NonZeroU*::add` to panic in release mode by deferring the check to `u*::checked_add`, but this is less explicit for the user than directly using `NonZeroU*::checked_add`.
Following `@Lokathor's` advice on zulip, I have dropped the idea.

`@poliorcetics` on Discord also suggested implementing `_sub` operations, but I'd postpone this to another PR if there is a need for it. My opinion is that it could be useful in some cases, but that it makes less sense because non-null natural numbers are not stable by subtraction even in theory, while the overflowing problem is just about technical implementation.

One thing I don't like is that the type of the `other` arg differs in every implementation: `_add` methods accept any raw positive integer, `_mul` methods only accept non-zero values otherwise the invariant is also broken, and `_pow` only seems to accept `u32` for a reason I ignore but that seems consistent throughout `std`. Maybe there is a better way to harmonize this?

This is it, Iope I haven't forgotten anything and I'll be happy to read your feedback.
2021-06-12 15:29:51 +00:00
..
alloc Remove "generic type" in boxed.rs 2021-06-12 04:11:48 +08:00
backtrace@221483ebaf Update backtrace to 0.3.57 2021-04-25 13:48:03 -07:00
core Auto merge of #82703 - iago-lito:nonzero_add_mul_pow, r=m-ou-se 2021-06-12 15:29:51 +00:00
panic_abort rustdoc: link to stable/beta docs consistently in documentation 2021-06-04 14:18:21 -04:00
panic_unwind rustdoc: link to stable/beta docs consistently in documentation 2021-06-04 14:18:21 -04:00
proc_macro Simplify proc_macro code using Bound::cloned(). 2021-06-08 16:31:50 +00:00
profiler_builtins Update cc 2021-05-24 23:34:12 +01:00
rtstartup
rustc-std-workspace-alloc
rustc-std-workspace-core
rustc-std-workspace-std
std Rollup merge of #86051 - erer1243:update_move_keyword_docs, r=Mark-Simulacrum 2021-06-10 11:02:13 +09:00
stdarch@1e05dffbee std: Stabilize wasm simd intrinsics 2021-06-10 19:42:05 -07:00
term rustdoc: link to stable/beta docs consistently in documentation 2021-06-04 14:18:21 -04:00
test Auto merge of #84863 - ABouttefeux:libtest, r=m-ou-se 2021-06-06 09:13:59 +00:00
unwind Support Android ndk versions r23-beta3 and up 2021-06-01 21:37:50 +02:00