Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage of cast when From is available #181

Open
tcharding opened this issue Mar 28, 2024 · 1 comment
Open

Usage of cast when From is available #181

tcharding opened this issue Mar 28, 2024 · 1 comment

Comments

@tcharding
Copy link
Member

Using From (or into()) is easier to read than using casts because it stops devs having to wonder if an data being lost.

We have a few places where we use casts to cast u8 to usize

            let log1 = LOG[self.0 as usize];
            let log2 = LOG[other.0 as usize];

Could be written as:

            let log1 = LOG[usize::from(self.0)];
            let log2 = LOG[usize::from(other.0)];
@apoelstra
Copy link
Member

Interesting. TIL that usize implements From<u8> and From<u16>. (But not From from any other numeric type, and I don't believe Into for any numeric types).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants