-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for no_std #58
Conversation
Interesting proposal to make the library not depend on I can see how this could be useful. For example, my C++ library got adapted into the Google Fuchsia OS kernel, and they had to modify the code heavily to fit in that environment. I am interested in keeping the code clean, minimizing changes, and personally understanding every line of code. Your relatively small changeset (total 23 lines added) is a good start. I have a preliminary question for you: Can I / should I change my code to use (A bunch of reference resources for myself from Googling "Rust no_std": |
Thank you for reply! I think it is possible to convert you crate to |
Interesting. What is a sensible way to return either a Also, I noticed that due to my use of |
You don't require to drop Removal of Also, from my point of view (just opinion), it will be more idiomatic to replace Regarding As example of |
I thought about this topic a couple of times in the past year and have some ideas to share. I mostly agree with what you said regarding members from All my "traditional" implementations of this QR Code generator library, including Java, Python, TypeScript, C++, and Rust, use "internal allocation". That means when somebody calls my qrcodegen library code, this library performs a bunch of dynamically sized memory allocations on the heap and frees its own memory before returning. This is not a problem for garbage-collected languages like Java, or for running C++ on a desktop computer with lots of memory. But in environments like microcontrollers with kilobytes of RAM or operating system kernels where dynamic memory allocation may be expensive or dangerous, I decided it would be best to avoid all heap allocations within my library. Instead, the caller would pass two writable buffers of appropriate size ("external allocation"), my library would allocate O(1) memory on the stack (which does not depend on the length or values of the data in any way), and I would manually manage the temporary scratch space. My library would only use the buffers passed in by the caller, and not allocate/deallocate anything else. This is exactly the approach I took for my C library. So I feel like the more correct answer is that for my C, C++, and Rust versions, if I could afford the effort to write and continually maintain the code, I would have two flavors - "no-alloc" and "heap-alloc" - in each language. For reference, my no-alloc C library works like this: #define MY_MAX_VERSION 7
#define BUF_LEN qrcodegen_BUFFER_LEN_FOR_VERSION(MY_MAX_VERSION)
uint8_t dataAndTemp[BUF_LEN] = {'H','e','l','l','o'};
uint8_t qr[BUF_LEN];
bool ok = qrcodegen_encodeBinary(dataAndTemp, 5, qr, qrcodegen_Ecc_HIGH,
qrcodegen_VERSION_MIN, MY_MAX_VERSION, qrcodegen_Mask_AUTO, false);
print qrcodegen_getModule(qr, x, y) ... I think you can imagine how it translates to Rust. As a bonus for Rust, I can return a // Note: qrcodegen_BUFFER_LEN_MAX = 3918;
let mut dataAndTemp = [0u8; 3918];
let mut qrBuffer = [0u8; 3918];
dataAndTemp[0] = 'H' as u8;
dataAndTemp[1] = 'e' as u8;
dataAndTemp[2] = 'l' as u8;
dataAndTemp[3] = 'l' as u8;
dataAndTemp[4] = 'o' as u8;
let qr: Option<QrCode> = QrCode::encode_binary(&mut dataAndTemp, 5, &mut qrBuffer, QrCodeEcc::HIGH,
QrCode::VERSION_MIN, QrCode::VERSION_MAX, QrCodeMask::AUTO, false);
// qr must retain a reference to qrBuffer, which will constrain its mutability and lifetime
print qr.get_module(x, y) ... |
Today's initial commit of a Rust no-heap no_std implementation: https://github.com/nayuki/QR-Code-generator/tree/d8ea85f2e2a62852217b71151029cee7cc019139 |
@nayuki Any idea when it will be published? |
@mcroad: I consider my rust-no-heap implementation a fringe one. It will not be published on crates.io. You will have to manually download the source from Git. |
Hello @nayuki!
Thank you for qrencode crate!
I have added some
cfg
s that allow this crate to be compiled for no_std (no OS) environment. It would be nice if you merge this PR.Let me know if you need some changes before merge.
Thanks in advance!