Closed not-matthias closed 1 year ago
Experimented a little bit and i think we could use the inventory
crate for feature registration.
it has a few downsides though:
@7ap what do you think?
I think this is a great solution! I'll need to take a look at inventory myself but from your screenshots it shows like it does what it needs to do.
Implemented both versions and I think it's easier to go for linkme
.
We can't just create the object, we need to pass the function so that we can create it at compile time.
use crate::{Feature, FeatureTrait};
pub struct Esp {
pub enabled: bool,
}
impl FeatureTrait for Esp {
fn new() -> Box<dyn FeatureTrait> {
Box::new(Self { enabled: false })
}
}
inventory::submit! {
Feature::new("ESP", Esp::new)
}
Here we also have the option to link one function, but it's much less verbose and doesn't require life-before-main. We can hide the functionality of linkme
so that they never even have to specify the initialization function.
use linkme::distributed_slice;
use crate::Feature;
pub struct Esp {
pub enabled: bool,
}
impl Feature for Esp {
fn new() -> Box<Self> {
Box::new(Self { enabled: false })
}
}
#[distributed_slice(crate::FEATURES)]
fn init_esp() -> Box<dyn Feature> {
println!("Init esp");
Esp::new()
}
Implemented the first draft. See:
@7ap Feedback is welcome.
Check if we can use this: https://crates.io/crates/inventory