You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a use case where it would be convenient to insert the default value on a call to get() or on immutable index. For example, suppose you want to store RefCells in a DefaultHashMap. You would presumably use the immutable access methods for the same reason you are using a RefCell. But then any interior mutation of the RefCell actually mutates self.default.
You can't just insert the default value in get(), because get() borrows self immutably. You also can't just put the backing HashMap in a RefCell, either, because RefCell returns a RefMut that wouldn't outlive the call to get(), and thus the lifetime of the reference extracted from the RefMut can't outlive the call to get() and so can't be returned.
The way to do achieve this is to put the backing HashMap in a RefCell and then use Ref::map(), which gives a Ref object that can be returned:
The user needs to choose the consequences of insert on read, or else they could end up accidentally trying to allocate the universe. Thus I propose the semantics be selectible, defaulting to insert on write. You'd have an enum:
// In the `default` module:enumInsertSemantics{OnWrite,OnRead}// In the client code:letmut map = defaultmap::HashMap::with_semantics(InsertSemantics::OnRead);
...// Might as well make it settable:map.set_semantics(InsertSemantics::OnWrite);
This is obviously a breaking change.
What do you think?
The text was updated successfully, but these errors were encountered:
Originally, I think I wanted to insert on reads as well. The problem was indeed that you only have a immutable reference, not an mutable one when calling get.
From your description it's still not entirely clear to me how you would implement it. If you can create a PR that shows it working, I'm definitely open to merging it. What the default should be I'm not sure yet. I guess it will matter a bit on the implementation.
I have a use case where it would be convenient to insert the default value on a call to
get()
or on immutable index. For example, suppose you want to storeRefCell
s in aDefaultHashMap
. You would presumably use the immutable access methods for the same reason you are using aRefCell
. But then any interior mutation of theRefCell
actually mutatesself.default
.You can't just insert the default value in
get()
, becauseget()
borrowsself
immutably. You also can't just put the backingHashMap
in aRefCell
, either, becauseRefCell
returns aRefMut
that wouldn't outlive the call toget()
, and thus the lifetime of the reference extracted from theRefMut
can't outlive the call toget()
and so can't be returned.The way to do achieve this is to put the backing
HashMap
in aRefCell
and then useRef::map()
, which gives aRef
object that can be returned:or something like that.
The user needs to choose the consequences of insert on read, or else they could end up accidentally trying to allocate the universe. Thus I propose the semantics be selectible, defaulting to insert on write. You'd have an enum:
This is obviously a breaking change.
What do you think?
The text was updated successfully, but these errors were encountered: