# Casting usize to generic in \`const fn\`

**URL:** <https://users.rust-lang.org/t/casting-usize-to-generic-in-const-fn/24252>\
**Category:** help\
**Created:** [January 16, 2019, 4:37pm UTC](https://users.rust-lang.org/t/casting-usize-to-generic-in-const-fn/24252 "2019-01-16T16:37:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mistodon](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mistodon/32/7133_2.png) [@mistodon](https://users.rust-lang.org/u/mistodon)\
**Post date:** [January 16, 2019, 4:37pm UTC](https://users.rust-lang.org/t/casting-usize-to-generic-in-const-fn/24252/1 "2019-01-16T16:37:22Z")

</div>

So I'm playing around with const fns. I want to cast the output of `std::mem::size_of::<T>()` to `T` for all int types. In a non-const fn, I can define a casting trait and implement it for each int type (or better yet, use the `num` crate) - but I can't do this in a const context.

Here's a playground example of what I mean: [Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c7ac7c2238ce17fd978f3f12aa3d9452)

So my question is: is there any way - however hacky and terrible - to cast a `usize` to a generic integer type T?

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/rustyyato/32/49247_2.png) [@RustyYato](https://users.rust-lang.org/u/RustyYato)\
**Post date:** [January 16, 2019, 5:08pm UTC](https://users.rust-lang.org/t/casting-usize-to-generic-in-const-fn/24252/2 "2019-01-16T17:08:12Z")

</div>

Generics with bounds can't be used in a `const fn`.

[https://github.com/rust-lang/rust/issues/53555](https://github.com/rust-lang/rust/issues/53555)

I can't think of way to do it just yet, without bounds there doesn't seem to be a way to do it.

---

<div class="post-metadata">

**Author:** ![ExpHP](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/exphp/32/13145_2.png) [@ExpHP](https://users.rust-lang.org/u/ExpHP)\
**Post date:** [January 16, 2019, 5:13pm UTC](https://users.rust-lang.org/t/casting-usize-to-generic-in-const-fn/24252/3 "2019-01-16T17:13:16Z")

</div>

Not unlike Moore's law, I believe the simple answer here is to wait. 🙂

(or contribute!)

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/scottmcm/32/4286_2.png) [@scottmcm](https://users.rust-lang.org/u/scottmcm)\
**Post date:** [January 16, 2019, 9:23pm UTC](https://users.rust-lang.org/t/casting-usize-to-generic-in-const-fn/24252/4 "2019-01-16T21:23:04Z")

</div>

You might also be interesting in the following discussion about core traits for these conversions:

[https://github.com/rust-lang/rfcs/pull/2484](https://github.com/rust-lang/rfcs/pull/2484)

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [January 12, 2023, 8:49am UTC](https://users.rust-lang.org/t/casting-usize-to-generic-in-const-fn/24252/5 "2023-01-12T08:49:55Z")

</div>


