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

add a take method which takes a mutable reference #100

Merged
merged 1 commit into from
May 13, 2020

Conversation

sunshowers
Copy link
Contributor

I use OnceCell extensively in my code to cache lazily computed data on
structs. Sometimes, I mutate the data -- in which case I have to invalidate
those caches.

This is of course possible to do by oneself, but it's slightly non-obvious
that the "once" bit of OnceCell only applies in the shared reference context.

@sunshowers sunshowers force-pushed the reset branch 3 times, most recently from c705ac2 to 603162d Compare May 4, 2020 22:22
@matklad
Copy link
Owner

matklad commented May 5, 2020

Hm, I am a bit on the fence about this... My first reaction is that mem::take should be good enough. My second thought is that Option::take is a thing....

What do you think about renaming reset to take?

@sunshowers
Copy link
Contributor Author

Sure, sounds reasonable to me for symmetry.

I use `OnceCell` extensively in my code to cache lazily computed data on
structs. Sometimes, I mutate the data -- in which case I have to invalidate
those caches.

This is of course possible to do by oneself, but it's slightly non-obvious
that the "once" bit of OnceCell only applies in the shared reference context.
@sunshowers sunshowers changed the title [impl] add a reset method which takes a mutable reference add a take method which takes a mutable reference May 5, 2020
@sunshowers
Copy link
Contributor Author

done, thanks!

@sunshowers
Copy link
Contributor Author

ah, looks like mem::take isn't on the MSRV 1.31.1. think the MSRV should be bumped to 1.40?

@matklad
Copy link
Owner

matklad commented May 13, 2020

I'd prefer not to bump MSRV unless necessary (really, I'd prefer to not bump MSRV at all, and just implement rust-lang/rfcs#2788). mem::take is just mem::replace(_, Default::default), so should not be hard to poly-fill.

@matklad
Copy link
Owner

matklad commented May 13, 2020

I'll fix this up myself, thanks for sending a PR and sorry for this taking so long to review :)

@matklad matklad merged commit 8a322d0 into matklad:master May 13, 2020
@matklad
Copy link
Owner

matklad commented May 13, 2020

Also, wow, this is the hundredth issue/pr... Didn't expect that from a relatively simple utility crate :)

@matklad
Copy link
Owner

matklad commented May 13, 2020

Published as 1.4.0: https://crates.io/crates/once_cell/1.4.0

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.

2 participants