raylib-rs icon indicating copy to clipboard operation
raylib-rs copied to clipboard

[Suggestion] Use Clippy to improve code

Open alexmozaidze opened this issue 2 years ago • 3 comments

Use Clippy in order to improve some parts of the library, notably, use a slice in functions instead of vectors. Why is that a problem? Well, this requires me to allocate a vector even if I could use an array, which could make the program slower. One example I have is with include_bytes! macro, sometimes I just want to hardcode some sprites into the binary, but due to how picky Image::load_image_from_mem is, I have to allocate a vector and then pass it to function.

alexmozaidze avatar Dec 09 '21 17:12 alexmozaidze

That's fair. I haven't used Clippy before and I'm not sure how to set it up. I'll have to see about that.

Dacode45 avatar Dec 10 '21 20:12 Dacode45

Also, I'm not sure if Clippy can even lint this, but some functions that logically should not accept negative values accept i32, like RaylibBuilder::size, which makes no sense at all since there are no monitors with negative width/height, it just crashes in runtime if you attempt to give it a negative value.

alexmozaidze avatar Dec 16 '21 17:12 alexmozaidze

Maybe use Into trait for functions? A lot of times I have to convert some value back and forth, making functions accept values that implement Into could make user experience more pleasant. AKA something like this:

fn example(n: impl Into<u32>) -> u32 {
    n.into()
}

and it tells you wether you're out of range or not at compile-time

example(255_u8); // Works
example(255_u64); // Might go out of bounds, thus `From<u64>` is not implemented for u32

alexmozaidze avatar Dec 16 '21 18:12 alexmozaidze