# Ergonomics of wrapping operations

**URL:** <https://internals.rust-lang.org/t/ergonomics-of-wrapping-operations/1756>\
**Category:** ideas (deprecated)\
**Created:** [March 25, 2015, 7:34pm UTC](https://internals.rust-lang.org/t/ergonomics-of-wrapping-operations/1756 "2015-03-25T19:34:00Z")\
**Posts on this page:** 1\
**Showing post:** 43

<div class="post-metadata">

**Author:** ![glaebhoerl](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/glaebhoerl/32/1978_2.png) [@glaebhoerl](https://internals.rust-lang.org/u/glaebhoerl)\
**Post date:** [October 23, 2015, 10:50am UTC](https://internals.rust-lang.org/t/ergonomics-of-wrapping-operations/1756/43 "2015-10-23T10:50:10Z")

</div>

FWIW, one of the major pain-points with `Wrapping<T>` is that heterogeneous operations - e.g. `T + Wrapping<T>` - are not permitted. This is because it’s not obvious whether such an operation should wrap or not. In different words, one of the operands essentially needs to be casted before performing the operation, and it’s not clear which one.

I’m now kind of leaning towards saying that `Wrapping<T>` should “win”: in other words, that we should have `impl Add<u32> for Wrapping<u32> { type Output = Wrapping<u32>; ... }` (and vice versa), and likewise for the other types. The intuition is this: the reason why “plain” `T` (e.g. `u32`) panics on over/underflow is not because this is well-known to be the behavior that’s desired by users of `u32`; rather, the reason is precisely that we _don’t_ have any information about the programmer’s intent, so our only reasonable option is to signal the unhandled circumstance by panicking. (This is also underscored by the fact that panicking only happens in debug builds: if we _knew_ that panicking is what the programmer explicitly _wants_, then we would do it _always_.)

Now in the case of `u32 + Wrapping<u32>`, if we think of it as a unification problem, then “no available information” unifies with “it should wrap” to yield “it should wrap”.

(Incidentally, for completeness’s sake, we should presumably also have `Saturating<T>`, `Checked<T>`, and `Overflowing<T>` for the other possible policies, with analogous `impl`s. And [see also](https://internals.rust-lang.org/t/implicit-widening-polymorphic-indexing-and-similar-ideas/1141).)

---

_[View the full topic](https://internals.rust-lang.org/t/ergonomics-of-wrapping-operations/1756)._
