#How to "as cast" generically?

12 messages · Page 1 of 1 (latest)

hasty mason
#

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8b39f6ec9bd456ed5bbaa48fcbd1c461
^ sort of the outline of what I'm trying to make
I know you cant just go "A as B" with generic types- the as operator doesn't support it. So instead I've found num::cast::AsPrimitive which seems to do the same thing, but as a trait. But I can't for the life of me figure out how to make it work- the trait bounds or whatever it is don't make any sense to me. (might be a problem with the trait?)

to preempt anyone asking about what this is for / x-y problem- this trait is going to be part of a bitfield crate/macro I'm working on

brittle hare
#

You'd do A.as_() instead of A as _

hasty mason
#

looks a lot messier than it probably could be but at least it's a different sort of error 😅

brittle hare
#

Unsafe code is still typechecked. Idk what you're trying to do but transmute is usually not the best way to do it.

hasty mason
#

trying to turn a fieldless enum into whatever type it's repr is basically. would like to know if there's a better way

jaunty locust
#

It seems like bytemuck is exactly what you want here

#

bytemuck slash zerocopy, idk

hasty mason
#

bytemuck might work, thanks

hasty mason
#

think i might have ran into something const generics makes difficult / impossible here