`set()` should return Result<&T, T>, not Result<(), T>
jyn514 opened this issue · comments
jyn commented
Say I want to write a function like this:
pub(crate) fn override_frontend(&self, init: impl FnOnce(&mut TestFrontend)) -> &TestFrontend { /* ... */ }
Here's the current approach I have to use:
let mut frontend = TestFrontend::new(&*self);
init(&mut frontend);
if self.frontend.set(frontend).is_err() {
panic!("cannot call override_frontend after frontend is initialized");
}
self.frontend.get().unwrap()
It would be nice to be able to remove the second unwrap:
let mut frontend = TestFrontend::new(&*self);
init(&mut frontend);
match self.frontend.set(frontend) {
Ok(f) => f,
Err(e) => panic!("cannot call override_frontend after frontend is initialized"),
}
but right now set()
doesn't return that data, so I can't.
Alex Kladov commented
The fully general signature would be Resut<&T, (&T, T)>. I think we should add this as an extra method (insert perhaps?).
Alex Kladov commented
cc #148
Alex Kladov commented
Ok, naming is hard. We have Option::insert and Option::get_or_insert, which do different things. I think OnceCell::try_insert would probably be the right name for semantics we want here.
Alex Kladov commented
try_insert
now exists