Looking for a good way to convert i8...i64 into u8...u64

Hi everyone,

I have found this topic, but it's not clear from it if there something changed in 2024 or not.

In my case, I need to turn any type into Vec<u8>, so I've implemented trait for this case. All works nice except for signed integers.

So, my questions are:

  1. Do I still need to use unsafe code to convert signed integers into unsigned (or there smth better exists for such cases).
  2. What is the difference when I just use as operator ? For example, I made this sandbox example and results are same when using both as and unsafe

What's wrong with using i32::to_le_bytes?

1 Like

WDYM "still"? You never needed unsafe for that. Built-in as casting has always been there, and TryFrom and (where applicable) From work just fine too.

1 Like

Ah, right, that's work. My fault. Thanks to all for answers !

I suggest marking @paramagnetic 's answer as the solution.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.