# On Casts and Checked-Overflow

**URL:** <https://internals.rust-lang.org/t/on-casts-and-checked-overflow/1710>\
**Category:** bikeshed (deprecated)\
**Created:** [March 12, 2015, 4:42pm UTC](https://internals.rust-lang.org/t/on-casts-and-checked-overflow/1710 "2015-03-12T16:42:16Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [March 12, 2015, 5:17pm UTC](https://internals.rust-lang.org/t/on-casts-and-checked-overflow/1710/2 "2015-03-12T17:17:31Z")

</div>

I think when not otherwise specified, it should always be the logical content that is considered, and not the representation. So a cast should be valid if and only if the casted value is within the range of the casted-to type. (This is your “Strict Range”, I think.) I think this is the spirit of our overflow-handling design (why should `-1i8 as u8` be valid and not `0u8 - 1`?), as well as of [some other ideas for improved ergonomics](http://internals.rust-lang.org/t/implicit-widening-polymorphic-indexing-and-similar-ideas/1141).

Last I saw, the `WrappingOps` trait had grown, or was intended to grow, methods such as `wrapping_as_u8()`, `wrapping_as_i8()`, `wrapping_as_u16()`, and so on for each primitive type. I think these are what should be used when anything other than “Strict Range” semantics is desired. (Whether there’s a way to make these less monomorphic and more ergonomic, e.g. some kind of `wrapping_as()` that’s generic over the target type, is an independent question, I think. Also perhaps we could make `foo as Wrapping<u8>` where `foo: Wrapping<i8>` work in the future, having the old semantics of `as` on the primitive types.)

---

_[View the full topic](https://internals.rust-lang.org/t/on-casts-and-checked-overflow/1710)._
