Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Returning owned types from Map #92

Open
edward-shen opened this issue Oct 7, 2023 · 1 comment
Open

Feature Request: Returning owned types from Map #92

edward-shen opened this issue Oct 7, 2023 · 1 comment

Comments

@edward-shen
Copy link

Hey! Thanks for the work on this -- It's super useful and I appreciate the technical effort.

I'm running into a bit of trouble that I don't think is currently resolvable by the current implementation. In short, I have the following:

struct GlobalConfig {
    config: Arc<ArcSwap<Config>>,
}

struct Config {
    sub_configs: HashMap<String, SubConfig>
}

struct SubConfig {} // implementation is unnecessary

I'm trying to implement the following function:

impl GlobalConfig {
    fn fetch_subconfig(&self, config_name: &str) -> Guard<&SubConfig> {
        // Doesn't work
        self.config.map(move |config| config.get(config_name))
    }
}

where I dynamically load a sub-config based on the provided config name.

The issue is that I don't think the current API offers an easy way to do. Map currently requires a function that returns a reference, while HashMap::get returns an owned type (Option<&T>). This leads to two lifetime issues.

Here's a playground link of my attempt: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=cd5ff805d13d091e47852cf29266fec0

and where I get stuck.

Is there a way to offer this? Would a signature of something like map_owned<'a>(&'a self, f: impl Fn(&'a Self) -> T + 'a) work?

The closest API I can get is where I always return a static reference to a default value (https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=fbf48553ec5d3e85d0cb6aeb1dcf0d5d)

but that's not very useful as I'd optimally work with Result in this projection (e.g. it would return something like Result<&'a T, E>.

@vorner
Copy link
Owner

vorner commented Oct 7, 2023

Hello

I confirm your observation that this doesn't exist. The idea of Map is that it simply „projects“ into the whole thing and doesn't actually store anything. I'm not entirely sure this can be done or, if it can, if there are some serious drawbacks, currently. But as Map doesn't use anything „special“ from inside, maybe you could sketch it out and we could decide if it fits into the crate in some place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants