Hello everybody,
I'm looking for a crate that has functionality to get values out of structs by type (e.g. a derive macros + helper traits).
Something like:
use get_value_by_type::*;
#[derive(GetValueByType)]
struct MyStruct {
value: i32,
}
let my_struct = MyStruct { value: 42 };
let my_i32: i32 = my_struct.get_value_by_type();
// or:
let my_i32 = my_struct.get_value_by_type::<i32>();
I know, the various typemap crates on crates.io offer this functionality, but as far as I know, they are all based on a HashMap-like type.
If everything is known at compile-time, these typemap
crates add avoidable overhead, therefore I'm looking for alternatives.
So far I've found the following crates, that would enable what I want:
- Frunk (see this interesting article, which also describes why someone would what this)
- Rovv (unfortunately, not used much)
These crates would work, but I'm pretty sure, that what I try to archive can be done in a simpler, more ergonomic way, with better error messages if something bad happens. As far as I know, the type-level programming magic that at least Frunk
seems to apply is known for horrible error messages (please correct me, if I'm wrong).
Relevant keywords seem to be Row Polymorphism
and Structural Typing
.
Getting values out of a struct by a type alone would be good enough, but (optionally) getting values via a field name
/ type
pair would be useful for cases where I want to store several values of the same type (and don't want to create a new type, manually). Frunk
offers this, btw.