SergioBenitez / state

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

calls in statics are limited to constant functions, tuple structs and tuple variants #8

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

See https://docs.rs/state/0.4.1/state/#usage; you must enable the const_fn feature.

P.S: Note that Rust nightly is no longer required.

SergioBenitez commented 3 years ago

0.4.2 was just released that makes this work on stable as well.

neilyoung commented 3 years ago

Yep. Already noticed. Thanks