jeremyletang / rust-sfml

SFML bindings for Rust
Other
638 stars 88 forks source link

Implement safer pixel data functions #322

Closed dogunbound closed 9 months ago

dogunbound commented 9 months ago

I believe that it is unnecessary for the user to use unsafe functions when it can easily be implemented with safe functionality. This is an example of doing it safely with a small abstraction. Rust is all about safe code, not unsafe code. Such a simple abstraction should be hidden from the user

Note not inside of commit: I haven't tested it. I'll test it soon and leave a comment that I have tested it's functionality.

This will be a breaking change, but I believe it is for the better. People using these functions will have to change their code and decide whether they want to use the unchecked or standard version of the function.

dogunbound commented 9 months ago

Ok, i might still be testing this by the time you see this @crumblingstatue

I'm uploading some debug code, so my cargo build can see it off my branch

dogunbound commented 9 months ago

Finally done testing. Here is the test code i used:

use sfml::graphics::{Color, Image};

fn main() {
    let mut image = Image::new(64, 64);

    for x in 0..64 {
        for y in 0..64 {
            match image.set_pixel(x, y, Color::RED) {
                Ok(_) => (),
                Err(e) => {
                    let error_message = format!("{}", e);
                    println!("{}", error_message);
                }
            };

            match image.pixel_at(x, y) {
                Some(_) => (),
                None => {
                    println!("no pixel at x:{} y:{}", x, y);
                }
            }
        }
    }
}
dogunbound commented 9 months ago

Cargo fmt caught something in src/window/context.rs also. I added it as a seperate commit. Thought I might as well include it into this PR since it was already up.

crumblingstatue commented 9 months ago

Thank you for your contribution! I merged it manually in 3c732e8516239d13fdacec7b79ecffcbc34d3707