Hi. How could you for exemple to calculate various CRC on the same data in a for loop ?
What I have written :
let crc_algos = ["CRC_16_ARC",
"CRC_16_CDMA2000",
"CRC_16_CMS",
"CRC_16_DDS_110",
"CRC_16_DECT_R",
"CRC_16_DECT_X",
"CRC_16_DNP",
"CRC_16_EN_13757"];
for algo_item in crc_algos {
let mut crc_algo : crc::Crc<u16> = crc::Crc::<u16>::new(&crc::algo_item); // Pickup CRC algo
let crc_result = crc_algo.checksum(&buffer); // CRC calculation
println!("algo = {};\tcrc_result = 0x{:02X}", algo_item, crc_result); // Show Result
};
The tedious problem is how to point to crc::constants from a string.
Best regards.
Hmm, Ok for array of crc constants. But in the for loop, I get this error of compilation :
129 | for algo_item in crc_algos {
| --------- binding `algo_item` declared here
130 | let mut crc_algo : crc::Crc<u16> = crc::Crc::<u16>::new(&algo_item);
| ---------------------^^^^^^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `algo_item` is borrowed for `'static`
...
133 | }
| - `algo_item` dropped here while still borrowed
stringify! operates on the literal tokens you give it.
Since crc::Algorithm doesn't implement Debug (and even if it did, it'd give you Algorithm { width: ... poly: ... etc.}) you need to handle the debug printing yourself.
Unfortunately, this basically becomes no easier than this:
You can craft a macro to reduce the repetition, but there no built-in way that I know of to debug print the constant names used to build a list.
So. I'll make 2 arrays (one with constant strings, other with associated crc::constants) or maybe an array of struct {constant_name, crc::constant}.
Thanks for your enlightenment.