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
OnceCell
mut
API extension
#194
Comments
My gut feeling is that this is a rather narrow use-case, and that it isn't worth to duplicate many API functions. The If we do want to add anything here, it would be, perhaps,
as |
Using
Well, better this than nothing yes.
I don't really understand this 😅 |
So, the reason why we need let cell: &OnceCell<T> = ...;
let r: &T = match cell.get() {
Some(it) => it,
None => {
cell.set(f());
cell.get().unwrap()
}
}; would be racy -- the However, if we have |
Oh, ok, I get it now. Imo it doesn't do harm to have them. But I understand that keeping simple is a good choice too. |
I think I am inclined to not add these for now! |
Motivation:
I've experience that sometimes you want to
init
a type and get a mutable reference to it. Right now the way of doing that is to use use any of the available API calls that can initialise the value, depending on the needs:get_or_init
get_or_try_init
try_insert
Then, discard the
&T
returned and useget_mut
to get a&mut T
. For example:Playground
let v = cell.get_mut().unwrap_or_else(|| unreachable!());
looks like a code smell.This, even if working, breaks a couple of things:
std
counts with itsmut
counterpart (see slices or vec)&mut T
at the time we needProposition:
Implement
mut
versions of the API calls already implemented. To get something like:The text was updated successfully, but these errors were encountered: