How to use thread_local for lazy_static

I want to make lazy_static variables to be thread local variables as follow:

thread_local! {
lazy_static! {
pub static ref MY_GLOBAL_RSA_KEY: MutStatic = MutStatic::new();
pub static ref MY_GLOBAL_CURVE_KEY: MutStatic = MutStatic::new();
pub static ref MY_GLOBAL_PRE_KEY: MutStaticpre::PreCryptoKey = MutStatic::new();
}
}

how can I use these?

1 Like

You don't need lazy_static, you can initialize the thread_local with MutStatic::new() directly.

Thank you.
I modify my source as follow:

thread_local! {
pub static ref MY_GLOBAL_RSA_KEY: MutStatic = MutStatic::new();
pub static ref MY_GLOBAL_CURVE_KEY: MutStatic = MutStatic::new();
pub static ref MY_GLOBAL_PRE_KEY: MutStaticpre::PreCryptoKey = MutStatic::new();
}

But I got following error message:

error: no rules expected the token MY_GLOBAL_RSA_KEY
--> CryptoLib/src/crypto_util/mod.rs:84:20
|
84 | pub static ref MY_GLOBAL_RSA_KEY: MutStatic = MutStatic::new();
| ^^^^^^^^^^^^^^^^^ no rules expected this token in macro call

Could you let me know how to change?

You don't need ref

pub static MY_GLOBAL_*: MutStatic = MutStatic::new()

Btw, you can use code fences,

```rust
// your code here
```

to format your code here

I changed like this:
thread_local! {
pub static MY_GLOBAL_RSA_KEY: MutStatic = MutStatic::new();
pub static MY_GLOBAL_CURVE_KEY: MutStatic = MutStatic::new();
pub static MY_GLOBAL_PRE_KEY: MutStaticpre::PreCryptoKey = MutStatic::new();
}

But I got other error message as flollow:
error[E0599]: no method named set found for type std::thread::LocalKey<mut_static::MutStatic<crypto_util::RsaCryptoKey>> in the current scope
--> CryptoLib/src/crypto_util/mod.rs:179:33
|
179 | let ret = MY_GLOBAL_RSA_KEY.set(my_key);
| ^^^

Please read the documentation for LocalKey, in it you will see that you need to access the contents of the thread-local using the LocalKey::with function.

MY_GLOBAL_RSA_KEY.with(|rsa_key| rsa_key.set(my_key));

Thank you so much.
I try to do