rust-bitcoin / rust-bech32

Bech32 format encoding and decoding
MIT License
93 stars 49 forks source link

Usage of cast when `From` is available #181

Closed tcharding closed 1 month ago

tcharding commented 5 months ago

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 commented 5 months ago

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).