| Crates.io | const-config-size |
| lib.rs | const-config-size |
| version | 0.2.4 |
| created_at | 2025-03-28 00:03:16.610789+00 |
| updated_at | 2025-03-28 00:43:28.880595+00 |
| description | read a const usize from an array length, map key count, or integer literal in a .json file |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1609026 |
| size | 11,920 |
This is intended to be used with keeping the size of bounded-size buffers in sync with a JSON config file, but can be used to read a usize const from almost any sort of JSON config.
let's say we have a config file config.json with these contents:
[ { "a": 1 }, { "a": 2 } ]
we'd read the array length of that as a const like this:
use const_config_size::const_config_size;
use smallvec::SmallVec;
// will be equal to 2
const CONFIG_ITEM_COUNT: usize = const_config_size!("config.json");
// an example of the original intended use - keeping SmallVecs from spilling to heap
type UniqueItemBuffer<T> = SmallVec<[T; CONFIG_ITEM_COUNT]>;
it would also work the same with this config file, since there are 2 top-level keys in the object:
{ "a": {}, "b": {} }
if your data is nested in a JSON object, you can provide the path as a .-separated string.
for instance, with this config.json file:
{
"data": {
"inner": [1, 2]
}
}
you would use this invocation to access data.inner:
use const_config_size::const_config_size;
const CONFIG_ITEM_COUNT: usize = const_config_size!(("config.json", "data.inner"));