SergioBenitez / state

A Rust library for safe and effortless global and thread-local state management.
Other
209 stars 13 forks source link

Sample error #9

Closed neilyoung closed 3 years ago

neilyoung commented 3 years ago

Hi, I'm trying to follow the example code here https://docs.rs/state/0.4.1/state/

The compiler complains

error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
 --> src/main.rs:7:49
  |
7 | static CONFIG: Storage<RwLock<Configuration>> = Storage::new();
  |                                                 ^^^^^^^^^^^^^^
error: aborting due to previous error; 1 warning emitted

Code:

use state::Storage;
use std::sync::RwLock;

struct Configuration {}

static CONFIG: Storage<RwLock<Configuration>> = Storage::new();

fn main() {
    let config = Configuration {
        /* fill in structure at run-time from user input */
    };

    // Make the config avaiable globally.
    CONFIG.set(RwLock::new(config));

    /* at any point later in the program, in any thread */
    let mut_config = CONFIG.get().write();
}

What am I missing?

SergioBenitez commented 3 years ago

Closing as dup.

neilyoung commented 3 years ago

Thanks. This unfortunately gives

![feature]` may not be used on the stable release channel

neilyoung commented 3 years ago

Anyway, don't bother. I will find alternatives Thanks

neilyoung commented 3 years ago

Maybe a combination with lazy_static works, not sure...

lazy_static! { pub static ref CONFIG: Storage<RwLock> = Storage::new(); }

SergioBenitez commented 3 years ago

Ah, this is a valid issue; the library should be upgraded. Will take care of it.

neilyoung commented 3 years ago

You mean you have a solution for the stable channel? This would be awesome....

SergioBenitez commented 3 years ago

@neilyoung Yes.