How to migrate to `LazyLock` from `lazy_static`

⚓ Rust    📅 2025-04-28    👤 surdeus    👁️ 5      

surdeus

Warning

This post was published 115 days ago. The information described in this article may have changed.

I've been using lazy_static on types which are not Send, but Sync, and when I tried to switch to LazyLock after updating a compiler I started getting cannot be sent between threads safely errors.

And, I mean, I do not intend to Send the value between threads!
Is there anything I can do to make this code compile?

Here is an example of what I am trying to achieve.

In the real world the code I am writing a library for is not thread-safe, and trying to invoke the functions on the code actually has static asserts verifyinig that the execution thread is correct (there are some other tricks like passing a sentinel which can only be retrieved on a lib thread) -- this allowed me to safely mark similar types Sync. I know I could have used thread_local, but ergonomically for users it was way better not having to deal with with.

I know I can achieve this with an additional layer of indirection, but it would be nice if I could avoid &static.
Would be glad for any help or comments!

1 post - 1 participant

Read full topic

🏷️ rust_feed