matklad / once_cell

Rust library for single assignment cells and lazy statics without macros
Apache License 2.0
1.84k stars 110 forks source link

add a `take` method which takes a mutable reference #100

Closed sunshowers closed 4 years ago

sunshowers commented 4 years ago

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.

matklad commented 4 years ago

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 commented 4 years ago

Sure, sounds reasonable to me for symmetry.

sunshowers commented 4 years ago

done, thanks!

sunshowers commented 4 years ago

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

matklad commented 4 years ago

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

matklad commented 4 years ago

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

matklad commented 4 years ago

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

matklad commented 4 years ago

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