Best practices for passing configuration profile into multiple functions

Hello,

I defined multiple configuration profiles as multiple conts with type NestedStructWithManyStructs (funny name just for the example), and I use the profile in multiple functions, as you can see in the example.

At this moment, I have 20 functions that look similar to the example - the first line retrieves a reference to the profile and destructures a few fields that are needed in that function.

Could you please tell me if there are any performance implications, best practices, or other recommendations for this? Or maybe a different way to retrieve the config?

Thank you.

const PROFILE_FOO: NestedStructWithManyStructs = NestedStructWithManyStructs { data: "foo", data2: "foo" };
const PROFILE_BAR: NestedStructWithManyStructs = NestedStructWithManyStructs { data: "bar", data2: "bar" };

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct NestedStructWithManyStructs<'a> {
    pub data: &'a str,
    pub data2: &'a str
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ConfigName {
    Foo,
    Bar
}

fn get_cf(name: &ConfigName) -> &NestedStructWithManyStructs {
    match *name {
        ConfigName::Foo => &PROFILE_FOO,
        ConfigName::Bar => &PROFILE_BAR
    }
}

fn do_something_1(foo: &str, cf: &ConfigName) {
    let NestedStructWithManyStructs { data, .. } = get_cf(&cf);
    // ...
}
fn do_something_2(foo: &str, cf: &ConfigName) {
    let NestedStructWithManyStructs { data2, .. } = get_cf(&cf);
    // ...
}
fn do_something_3(foo: &str, cf: &ConfigName) {
    let NestedStructWithManyStructs { data, data2 } = get_cf(&cf);
    // ...
}

No, there is nothing expensive you've shown.

What you have looks fine. If all 20 functions use different parts of the config, I'm not sure you can do better. If multiple functions use the same parts of the config, you could have variants of get_cf that return exactly what is needed to avoid a pattern match, but even that would probably be about the same.

1 Like

Thank you.