-
Notifications
You must be signed in to change notification settings - Fork 20
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
LazyLock/Cell
get_mut()
and DerefMut
#429
Comments
If you're going to add the inherent method it should take The name should be |
@kennytm Ah yes, you're right about About the name... |
In other interior-mutable containers their
If you add a
|
@kennytm Oh! I didn't see that in once_cell it returns |
We discussed this in today's @rust-lang/libs-api meeting, and approved these. We'd like to have these, and we'd also like to have |
Proposal
Problem statement
When porting rust-analyzer once_cell code to use std's lazy types, one place used
&mut LazyCell<T>
and wanted to get a&mut T
from it (initializing if not initialized yet). once_cell providesDerefMut
andget_mut()
for that on itsLazy
types, but std has no way to mutably access the contents of aLazyLock/Cell
.Motivating examples or use cases
The code I ported is here: https://github.com/rust-lang/rust-analyzer/blob/91aa3f46b32032d7d62c4e94e4ea973f63aacc8f/crates/hir-def/src/generics.rs#L600
That code could use
Option
(this is what I transformed it into), but that means having to carry around the initializer every time. Alternatively, there could be aLazyMut
type, but the role seems to fitLazyCell/Lock
perfectly and there is even no need for perf compromises. Also, even though I don't have a use-case for that, maybe someone need to access aLazy
sometimes mutably and sometimes immutably.Solution sketch
Alternatives
We can provide only
get_mut()
or onlyDerefMut
. I choose to provide both becauseDerefMut
is easier to work with, butget_mut()
is a standard in interior mutable types.This could be emulated using
Option
, as I said.Links and related work
https://docs.rs/once_cell/latest/once_cell/sync/struct.Lazy.html#method.get_mut
https://docs.rs/once_cell/latest/once_cell/sync/struct.Lazy.html#impl-DerefMut-for-Lazy%3CT,+F%3E
https://docs.rs/once_cell/latest/once_cell/unsync/struct.Lazy.html#method.get_mut
https://docs.rs/once_cell/latest/once_cell/unsync/struct.Lazy.html#impl-DerefMut-for-Lazy%3CT,+F%3E
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: