mehcode / config-rs

⚙️ Layered configuration system for Rust applications (with strong support for 12-factor applications).
Apache License 2.0
2.57k stars 213 forks source link

[feature request] zero cost, zero sized default config types? #229

Open makoConstruct opened 3 years ago

makoConstruct commented 3 years ago

I'm not sure whether this is compatible with the way you do things, but I've found myself in need of zero cost config types in the course of making somewhat complex data structures, there are lots of little variables that need to be tweaked and tuned and experimented with at runtime, but ultimately will end up being constant in most applications. It turns out that Rust can support that really elegantly with zero-sized types: https://github.com/makoConstruct/vec-with-gaps/blob/main/src/lib.rs#L243 and the stdlib has started doing a similar thing with allocators.

I'm thinking about writing codegen for doing these, so I went looking to see if anyone else was already doing it. It doesn't seem like you're doing that kind of thing? (with the Loosely Typed stuff?) But I thought I should ask you, in case there's some route to compatability I can't see or in case you know of another project doing something closer to this.

matthiasbeyer commented 3 years ago

Hi. I don't understand what you are trying to do. A config needs to parse some configuration source, either a file or a string or something like that, so that is never zero cost (as with the example you linked), because you have to have data somewhere! Or am I misunderstanding something here?

makoConstruct commented 3 years ago

These configs can be zero cost when and only when the user wants a config consisting of constants specified in code, usually the default values.

In my example, the configs can be constant, or they can be non-zero-cost from files (although I haven't written one for that yet), depending on which concrete type implementing VWGConfig is used. Either would have the same API from the data structure's perspective.

makoConstruct commented 2 years ago

A config needs to parse some configuration source, either a file or a string or something like that, so that is never zero cost

I think this will clarify this a bit: This is not true of defaults. Defaults can be hard-coded, and even indexing-by-value accesses can sometimes be inlined out if the index is known at compile time.

In my game development workflows, it's common for something to start as a dynamic setting loaded from a file, and then become a fixed default once I've tweaked it a bit and decided on the perfect setting. I also expect to see that workflow with some parameters in data structures.

matthiasbeyer commented 2 years ago

Hi. Sorry that I let stall this. In my "re-thinking config-rs" work, I am trying to get to a setup where we do not copy around data anymore. If you like to check out my ideas, have a look at #321 ... there's a branch linked somewhere... but be told that this is highly experimental and I only find little time to work on it - contributions still welcome of course!