Skip to content

stella3d/const-config-size

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

const-config-size

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.

usage example

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": {} } 

field nesting

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"));

About

read an array config file at compile time and save the number of elements in it

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages