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

Support initialization with manual value? #160

Open
intgr opened this issue Nov 20, 2019 · 1 comment
Open

Support initialization with manual value? #160

intgr opened this issue Nov 20, 2019 · 1 comment

Comments

@intgr
Copy link

intgr commented Nov 20, 2019

I have a use case that's similar to lazy_static but that cannot be solved nicely right now.

If the default initializer to a lazy_static variable is something that wants arguments at initialization-time, or is something that can fail, then currently the usage is very awkward. Of course it's possible that I'm missing something obvious, I'm very new to Rust still, so apologies in advance if that's the case.

You may well consider this out of scope for this project (wouldn't really be "lazy"), but I wanted to ask before I contemplate if it's worth forking the project.

Basically, what I want would look something like this:

lazy_static! {
    pub static ref QUOTES: QuoteLibrary;
}
fn main() {
    let filename = "quotes.yaml";
    match QuoteLibrary::load_file(filename) {
        //                        ^^^^^^^^
        // allows me to provide arguments to the initializer function
        Ok(quotes) => lazy_static::initialize(&QUOTES, quotes); // assigned here
        Err(err) => // allows reporting initialization failures nicely
    }
}

The semantics would be:

  • Any access to QUOTES before it's initialized would panic.
  • Attempting to set the value multiple times might possibly panic too?

This saves me from writing:

lazy_static! {
    static ref QUOTES: Mutex<Option<QuoteLibrary>> = Mutex::new(None);
}

and also saves .lock().unwrap() calls every time I want to use the QUOTES value.

For context, this is what I initially wrote, when attempting to use the current lazy_static:

lazy_static! {
    pub static ref QUOTES: QuoteLibrary = QuoteLibrary::load_file_or_exit("quotes.yaml");
    //              Note: cannot supply arguments from main() function.   ^^^^^^^^^^^^^
}
fn main() {
    lazy_static::initialize(&QUOTES);
    // other code
}
impl QuoteLibrary {
    pub fn load_file_or_exit(filename: &str) -> QuoteLibrary {
        match Self::load_file(filename) {
            Ok(quotes) => quotes,
            Err(err) => {
                println!("Error loading {}: {}", filename, err);
                exit(1);   // <-- Calling exit() from an initializer is awkward
            }
        }
    }
}
@pravic
Copy link

pravic commented Jun 24, 2020

Check this for your case: https://docs.rs/once_cell/1.4.0/once_cell/

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