# Missing traits for checked arithmetic

**URL:** <https://internals.rust-lang.org/t/missing-traits-for-checked-arithmetic/15597>\
**Category:** Uncategorized\
**Created:** [November 12, 2021, 9:14pm UTC](https://internals.rust-lang.org/t/missing-traits-for-checked-arithmetic/15597 "2021-11-12T21:14:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dpc/32/97_2.png) [@dpc](https://internals.rust-lang.org/u/dpc)\
**Post date:** [November 12, 2021, 9:14pm UTC](https://internals.rust-lang.org/t/missing-traits-for-checked-arithmetic/15597/1 "2021-11-12T21:14:52Z")

</div>

While working on [GitHub - dpc/overflow-proof: Monadic checked arithmetic for Rust](https://github.com/dpc/overflow-proof/) I was disappointed that there are no traits abstracting over checked arithmetic in stdlib.

There's a lot of [`checked_*` functions](https://doc.rust-lang.org/std/num/struct.Wrapping.html?search=checked%5D) but no glue to allow handling them in a generic way.

From my PoV if there's anything that stdlib is useful for is establishing interoperability interfaces between 3rd party libraries. Would be nice if I could write a overflow handling library that would work for any time that implements a common trait.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [November 12, 2021, 10:57pm UTC](https://internals.rust-lang.org/t/missing-traits-for-checked-arithmetic/15597/2 "2021-11-12T22:57:48Z")

</div>

The stdlib is intentionally short of a whole bunch of commonly-requested traits for math. There's no trait for the additive identity element either, for example.

The general answer for this is that you want [num\_traits - Rust](https://docs.rs/num-traits/0.2.14/num_traits/index.html), which has `CheckedAdd` and friends, as well as many others.

---

<div class="post-metadata">

**Author:** ![dpc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dpc/32/97_2.png) [@dpc](https://internals.rust-lang.org/u/dpc)\
**Post date:** [November 13, 2021, 4:42am UTC](https://internals.rust-lang.org/t/missing-traits-for-checked-arithmetic/15597/3 "2021-11-13T04:42:42Z")

</div>

> [@scottmcm](#):
>
> The general answer for this is that you want [num\_traits - Rust](https://docs.rs/num-traits/0.2.14/num_traits/index.html), which has `CheckedAdd` and friends, as well as many others.

Thanks. I'll convert my ad-hoc traits to this then.

Is this official semi-official stance? Is it/should be documented somewhere?

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quinedot/32/7294_2.png) [@quinedot](https://internals.rust-lang.org/u/quinedot)\
**Post date:** [November 13, 2021, 5:38am UTC](https://internals.rust-lang.org/t/missing-traits-for-checked-arithmetic/15597/4 "2021-11-13T05:38:28Z")

</div>

> [@dpc](#):
>
> Is this official semi-official stance? Is it/should be documented somewhere?

It is the official semi-official stance. The Rust teams don't like to officially recommend crates so far. Including ones that spun out of core (pre-1.0) Rust originally, like `num_traits`, or are otherwise semi-official, which is why it's not in the official documentation.

---

<div class="post-metadata">

**Author:** ![bascule](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bascule/32/3057_2.png) [@bascule](https://internals.rust-lang.org/u/bascule)\
**Post date:** [November 13, 2021, 2:59pm UTC](https://internals.rust-lang.org/t/missing-traits-for-checked-arithmetic/15597/5 "2021-11-13T14:59:46Z")

</div>

There''s also the `checked` crate which introduces a `Checked` type similar to `core::num::Wrapping`: [checked::Checked - Rust](https://docs.rs/checked/0.5.0/checked/struct.Checked.html)

It provides a pretty good example of what a self-contained addition of checked arithmetic wrappers might look like if added to the standard library.

There was a pre-RFC about it posted here cc @pdolezal: [Where is std::num::Saturating? (Going to pre-RFC!) - #27 by pdolezal](https://internals.rust-lang.org/t/where-is-std-saturating-going-to-pre-rfc/13053/27)

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [November 13, 2021, 8:50pm UTC](https://internals.rust-lang.org/t/missing-traits-for-checked-arithmetic/15597/6 "2021-11-13T20:50:10Z")

</div>

> [@dpc](#):
>
> Is this official semi-official stance?

Well, here's the pre-1.0 PR that moved all this stuff out of the standard library and created the `num` crate:

> <https://github.com/rust-lang/rust/pull/17309>
>
> This is part of the migration of crates into the Cargo ecosystem. There
> is now a…n external repository https://github.com/rust-lang/num for bignums.
> 
> The single use of libnum elsewhere in the repository is for a shootout
> benchmark. This commit does not attempt to remove that usage for now.
> 
> Due to deprecation, this is a:
> 
> \[breaking-change\]

You'll see that getting to it through rust-lang still works -- try [https://github.com/rust-lang/num](https://github.com/rust-lang/num) -- but it's been moved to a separate github organization officially.

So yeah, it's _de facto_ official.

My reading of the tea leaves is that we might get `num::Checked` in std, like there's `num::Wrapping`, but that we're unlikely to have `CheckedAdd`+`CheckedSub`+... in std.

(That also follows the "add a type instead of a trait" convention we seem to be developing. Like you could then bound on `Checked<T>: Add` instead of `T: CheckedAdd`.)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [February 11, 2022, 8:50pm UTC](https://internals.rust-lang.org/t/missing-traits-for-checked-arithmetic/15597/7 "2022-02-11T20:50:49Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
