nayuki / QR-Code-generator

High-quality QR Code generator library in Java, TypeScript/JavaScript, Python, Rust, C++, C.
https://www.nayuki.io/page/qr-code-generator-library
5.2k stars 1.11k forks source link

Changed Version::new to const in Rust code. #174

Closed mmilata closed 1 year ago

mmilata commented 1 year ago

Awesome library, appreciate the rust-no-heap version, it's working great! I'd like to define local max version, since anything larger doesn't fit on the device screen:

const QR_MAX_VERSION: Version = Version::new(9);
...
let mut outbuffer = [0u8; QR_MAX_VERSION.buffer_len()];
let mut tempbuffer = [0u8; QR_MAX_VERSION.buffer_len()];

For that it would be useful if Version::new was const. Unforunately it's not only a matter of adding the keyword since RangeInclusive::contains is not (yet?) const.

nayuki commented 1 year ago

I see. These are both good features - using range-contains versus supporting const. I think the range-contains situation will resolve itself eventually, so I'll add the const keyword to Version::new() once that is possible.

If you need the functionality immediately, you're better off maintaining a private fork/patch.

mmilata commented 1 year ago

Fair enough, I can just define the array size constant directly instead of computing it from version, it's not as nice but not good enough reason for private fork.

nayuki commented 1 year ago

I gave you suggestion some thought and I see the merit of your use-case. Indeed, I wanted to support something like Version::new(10).buffer_len() and use that to size an array variable whose length is fixed at compile time. This is something I supported in the C port, and kind of supported in the Rust port with Version::MIN and Version::MAX... but those work because they construct using Version(1) and Version(40) using the internal private mechanism, which is why they don't have the same problem that Version::new() has.

Commits https://github.com/nayuki/QR-Code-generator/commit/5d9ec8dfdddbd66e14833130aa849b7ca5a894e1 , https://github.com/nayuki/QR-Code-generator/commit/2643e824eb15064662e6c4d99b010740275a0be1 .

mmilata commented 1 year ago

Thank you!