Skip to content
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

Replace lazy_static! macro with once_cell #1711

Merged
merged 1 commit into from
Jan 27, 2020

Conversation

dralley
Copy link
Contributor

@dralley dralley commented Jan 27, 2020

once_cell is generally considered to be superior to lazy_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 the once_cell API.

rust-lang-nursery/lazy-static.rs#111
rust-lang/rfcs#2788

Copy link
Member

@coolreader18 coolreader18 left a 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()));
Copy link
Member

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()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably!

Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

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

@dralley dralley force-pushed the once-cell branch 4 times, most recently from 33fe2c7 to 42c521c Compare January 27, 2020 05:59
@coolreader18 coolreader18 merged commit dc98ea6 into RustPython:master Jan 27, 2020
@dralley dralley deleted the once-cell branch January 27, 2020 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants