kornelski / imgref

A trivial Rust struct for interchange of pixel buffers with width, height & stride
https://lib.rs/crates/imgref
Apache License 2.0
59 stars 6 forks source link

Why are the fields public? #1

Open quadrupleslap opened 6 years ago

quadrupleslap commented 6 years ago

After all the assertions in the code, it's weird that the fields are public, since anyone could change them right afterwards, placing the object in an invalid state. Maybe make it harder for people to shoot themselves in the foot by using private fields and getters (and maybe setters, if necessary)?

kornelski commented 6 years ago

The dimension fields are meant to be read-only. It is unfortunate that this is not enforced.

When you call a method with &self, Rust borrows the entire object. This means it's not possible to get a writeable buffer via fn buf(&mut self) and then read dimensions via fn width(&self), since the first call "locks" the entire object.

quadrupleslap commented 6 years ago

Fair enough, but why can't the user read the width first if they need it, and then write? Or why not proxy the width and height getters through the writable buffer? This (probably useless) flexibility doesn't seem worth it.

kornelski commented 6 years ago

OK, I think I can make it work with enough high-level functionality added, so that it will be rare to need to read these properties.

I've added indexing img[(x,y)], so that img.buf[y*img.stride + x] is not needed.

kornelski commented 6 years ago

pixels and rows iterators are done.

I still use raw buf to: