Skip to content

Commit f40d825

Browse files
authored
Rollup merge of #149305 - tisonkun:oncecell-simplify, r=chenyukang
Simplify OnceCell Clone impl Noticed when developing fast/mea#79. This may also apply to `OnceLock`. But unfortunately, we can't construct a completed `Once` beforehand. Note that `OnceCell::from` is marked as [`rustc_const_unstable`](#143773) so we don't need an extra `const fn from_value` to leverage possible const convert benefit.
2 parents ac7b0bd + a700e47 commit f40d825

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

library/core/src/cell/once.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -376,14 +376,10 @@ impl<T: fmt::Debug> fmt::Debug for OnceCell<T> {
376376
impl<T: Clone> Clone for OnceCell<T> {
377377
#[inline]
378378
fn clone(&self) -> OnceCell<T> {
379-
let res = OnceCell::new();
380-
if let Some(value) = self.get() {
381-
match res.set(value.clone()) {
382-
Ok(()) => (),
383-
Err(_) => unreachable!(),
384-
}
379+
match self.get() {
380+
Some(value) => OnceCell::from(value.clone()),
381+
None => OnceCell::new(),
385382
}
386-
res
387383
}
388384
}
389385

0 commit comments

Comments
 (0)