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

Clean up OnceCell #3945

Merged
merged 4 commits into from Jul 12, 2021
Merged

Clean up OnceCell #3945

merged 4 commits into from Jul 12, 2021

Conversation

Darksonn
Copy link
Contributor

@Darksonn Darksonn commented Jul 8, 2021

This cleans up the OnceCell a bit.

@Darksonn Darksonn added A-tokio Area: The main tokio crate M-sync Module: tokio/sync labels Jul 8, 2021
@carllerche carllerche requested a review from hawkw July 9, 2021 19:46
@carllerche
Copy link
Member

@hawkw are you able to take a look at this one? ❤️

@hawkw
Copy link
Member

hawkw commented Jul 9, 2021

@hawkw are you able to take a look at this one? heart

sure, giving it a look!

Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

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

this looks great --- the code is much clearer, and the docs are much better IMO. 👍

///
/// [`SetError::AlreadyInitializedError`]: crate::sync::SetError::AlreadyInitializedError
/// [`SetError::InitializingError`]: crate::sync::SetError::InitializingError
/// ['OnceCell::get_or_init`]: crate::sync::OnceCell::get_or_init
pub fn set(&self, value: T) -> Result<(), SetError<T>> {
if !self.initialized() {
Copy link
Member

Choose a reason for hiding this comment

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

take it or leave it: might read a little better if this was

if self.initialized() {
    return Err(SetError::AlreadyInitializedError(value));
}

match self.semaphore.try_acquire() {
   // ...
}

but, take it or leave it

///
/// This will deadlock if `f` tries to initialize the cell itself.
/// This will deadlock if `f` tries to initialize the cell recursively.
Copy link
Member

Choose a reason for hiding this comment

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

could be nice to have an example here, but not a blocker.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, then I would want a normal example too. We can't have the only example be how to do it incorrectly.

///
/// This will deadlock if `f` tries to initialize the cell itself.
/// This will deadlock if `f` tries to initialize the cell recursively.
Copy link
Member

Choose a reason for hiding this comment

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

similarly, an example might be nice.

@Darksonn Darksonn merged commit 80d8d40 into master Jul 12, 2021
@Darksonn Darksonn deleted the oncecell-cleanup branch July 12, 2021 09:19
@Darksonn Darksonn mentioned this pull request Jul 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate M-sync Module: tokio/sync
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants