-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Replace lazy_static! macro with once_cell #1711
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
vm/src/vm.rs
Outdated
lazy_static! { | ||
static ref REPR_GUARDS: Mutex<HashSet<usize>> = { Mutex::new(HashSet::new()) }; | ||
} | ||
static REPR_GUARDS: Lazy<Mutex<HashSet<usize>>> = Lazy::new(|| Mutex::new(HashSet::new())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be Lazy::default()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately I don't think so. I don't see anything like that documented and throws a compile error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really? It looks like Default
is implemented for Lazy<T: Default>
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, maybe it's not a const fn
. Could do Lazy::new(Mutex::default)
, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, that's it. But the latter strategy works
33fe2c7
to
42c521c
Compare
once_cell
is generally considered to be superior tolazy_static
nowadays since it doesn't require a macro and has some extra flexibility. The ongoing proposal to add lazy initialization to the stdlib basically copies theonce_cell
API.rust-lang-nursery/lazy-static.rs#111
rust-lang/rfcs#2788