Fluent temporary message value

pub struct Ftl {
    // ...
    m_assets: Arc<RwLock<HashMap<Locale, Arc<fluent::FluentBundle<fluent::FluentResource>>>>>,
    // ...
}

impl Ftl {
    fn get_message_by_locale(&self, id: &str, locale: Locale) -> Option<FluentMessage> {
        if let Some(assets) = self.m_assets.read().unwrap().get(&locale) {
            if let Some(message) = assets.get_message(id) {
                return Some(message);
            }
        }
        // ...
    }
}

The line return Some(message); is giving:

cannot return value referencing temporary value
returns a value referencing data owned by the current function rustc Click for full compiler diagnostic

ftl.rs(281, 31): temporary value created here

cannot return value referencing temporary value
returns a value referencing data owned by the current function rustc Click for full compiler diagnostic

ftl.rs(281, 31): temporary value created here

It is not possible to return a reference to something behind an RwLock. Just think about it – if you could, then how would the lock be able to track who is currently accessing (reading or writing) the locked value? You must return the lock guard if you don't want to clone the contents to be returned.

(Cloning wouldn't help in this case, either, since FluentMessage apparently contains a lifetime parameter that you didn't indicate in the signature – you should definitely include it, by the way.)

1 Like

My method was for indirectly retrieving Fluent FTL messages. I decided to just unify all message retrieving methods into get_message for now, which'll invoke format_pattern together...

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.