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

Readme clashes with Backend trait contract #44

Open
urkle opened this issue Jul 24, 2023 · 0 comments
Open

Readme clashes with Backend trait contract #44

urkle opened this issue Jul 24, 2023 · 0 comments

Comments

@urkle
Copy link
Contributor

urkle commented Jul 24, 2023

The Backend trait contract returns Option<&str> in version 2.0, however the README shows the example of the RemoveI18n that returns Option instead.

Currently I'm trying to create a "reloadable backend". where I can setup a watcher via the Notify create to reload the translations from disk. However I am running into issues due to the contract change of Backend as I am unable to return a reference to a string.

struct ReloadableBackend {
    translations: Arc<RwLock<HashMap<String, HashMap<String, String>>>>,
}

impl Backend for ReloadableBackend {
   fn available_locales(&self) -> Vec<&str> {
        let l = self.i18n.read();
        if let Ok(h) = l {
            let mut locales = h.keys().map(|k| k.as_str()).collect::<Vec<_>>();
            locales.sort();
            locales // Error cannot return value referencing local variable `h` [E0515] returns a value referencing data owned by the current function
        } else {
            Vec::new()
        }
    }

    fn translate(&self, locale: &str, key: &str) -> Option<&'_ str> {
        let l = self.i18n.read();
        if let Ok(h) = l {
            return h
                .get(locale)
                .and_then(|trs| trs.get(key))
                .and_then(|k| Some(k.as_str())); // cannot return value referencing local variable `h` [E0515] returns a value referencing data owned by the current function
        } else {
            None
        }
    }
}

This, of course, makes perfect sense since it has to return a reference from the locked data in the Arc.

If the contract were still returning String then this would not be an issue as I could just return a cloned string and move on.

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

1 participant