Closed daxpedda closed 3 years ago
I am torn on this one. One the one hand, set
not returning a reference to a value is definitelly a loss of generality. On the other hand, the fully general signature here would be -> Result<&T, (&T, T)>
. On the third hand, it feels like .set
, .get
and .get_or_init
give the necessary tools to express this without adding extra methods.
Anothe option would be to add a simpler fn insert(&self, value: T) -> &T
methond, a-la Option<Insert>
.
On the other hand, the fully general signature here would be
-> Result<&T, (&T, T)>
.
I don't mind implementing it like this if you want.
On the third hand, it feels like
.set
,.get
and.get_or_init
give the necessary tools to express this without adding extra methods.
So the only reason .get_or_init
doesn't cover this for me is because it doesn't tell me if it was already initialized. I want an error if it's already initialized. So I'm kinda forced to use .set
and .get
to achieve this.
Anothe option would be to add a simpler
fn insert(&self, value: T) -> &T
methond, a-laOption<Insert>
.
Same as above, it doesn't solve my use-case.
Sorry, I think I should have explained my use-case more clearly in the OP.
I explained my use-case in OP more clearly now.
should be covered by #151
This adds a function called
set_and_get
that returns the value on setting it. Originally I wanted to implement it only forsync
, because the value was just being discarded, see the comment I added tounsync::OnceCell::set_and_get
.Is there interest in a feature like this?
EDIT: My use case is to initialize but return an
Err
if it already is initialized. This can be done by callingOnceCell::set
and thenOnceCell::get
, this offers an optimization, that culls this to only one function call. This can't be solved by usingOnceCell::get_or_init
because it doesn't offer any indication if it was already set.