Skip to content

Use once_cell instead of lazy_static #416

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

Merged
1 commit merged into from
Oct 30, 2019
Merged

Conversation

wusyong
Copy link
Contributor

@wusyong wusyong commented Oct 30, 2019

once_cell provides a neat way of initializing lazy singletons without
macro. This PR use sync::Lazy to streamline same pattern proposed in
related rust RFC.

Resolve #406

`once_cell` provides a neat way of initializing lazy singletons without
macro. This PR use `sync::Lazy` to streamline same pattern proposed in
related rust RFC.

Resolve async-rs#406
@ghost ghost requested a review from matklad October 30, 2019 11:12
static ref POOL: Pool = {
let num_threads = num_cpus::get().max(1);
let mut stealers = Vec::new();
static POOL: Lazy<Pool> = Lazy::new(|| {
Copy link
Member

Choose a reason for hiding this comment

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

As this doesn't really make heavy use of Deref for Lazy, a more minimal and direct approach would be to use OnceCell.

static POOL: OnceCell<Pool> = OnceCell::new();
POOL.get_or_init(|| ...)

But, as we are using Lazy elsewhere, I guess it's better to use it here as well, just for consistency

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I make use of Lazy mostly to streamline with on-going rust RFC. I think this will be easier for later maintenance.

Copy link
Member

Choose a reason for hiding this comment

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

The RFC proposes both OnceCell and Lazy, as OnceCell is strictly more powerful.

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Thank you! :)

@ghost ghost merged commit ff6a44f into async-rs:master Oct 30, 2019
@wusyong wusyong deleted the once_cell branch October 30, 2019 11:25
This pull request was closed.
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.

use once_cell instead of lazy_static
2 participants