Add Borrow
and AsRef
implementations to LazyCell
and LazyLock
#477
Labels
Borrow
and AsRef
implementations to LazyCell
and LazyLock
#477
Proposal
Problem statement
LazyCell
andLazyLock
are types that greatly simplify (often static) initialization, allowing the programmer to not worry about when a value is initialized, and simply treat it as if it is always initialized instead. While these types do implement theDeref
trait, they lackAsRef
andBorrow
implementations. This can lead to a lot of&*
prefixing in places where it could be avoided.Motivating examples or use cases
As stated, the goal is to reduce the amount of
&*
prefixing. This is usually accomplished by asking for aimpl AsRef<T>
orimpl Borrow<T>
instead of&T
directly. By implementing these traits forLazyCell
andLazyLock
, they will also be compatible with this technique.One might argue that
AsRef
andBorrow
should be cheap, and the first call to them might call a heavy function. But I believe that ship has already sailed, since they both already implementDeref
.Solution sketch
Add
impl<T> AsRef<T> for LazyCell<T>
andimpl<T> AsRef<T> for LazyLock<T>
, which both probably just call theDeref::deref
to not have to deal with any internals.Alternatives
I can't really think of any alternatives, other than keeping the more verbose
&*
prefixes.Links and related work
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: