Rollup merge of #149305 - tisonkun:oncecell-simplify, r=chenyukang

Simplify OnceCell Clone impl

Noticed when developing https://github.com/fast/mea/pull/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`](https://github.com/rust-lang/rust/issues/143773) so we don't need an extra `const fn from_value` to leverage possible const convert benefit.
This commit is contained in:
Matthias Krüger
2025-11-25 17:51:19 +01:00
committed by GitHub
+3 -7
View File
@@ -376,14 +376,10 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
impl<T: Clone> Clone for OnceCell<T> {
#[inline]
fn clone(&self) -> OnceCell<T> {
let res = OnceCell::new();
if let Some(value) = self.get() {
match res.set(value.clone()) {
Ok(()) => (),
Err(_) => unreachable!(),
}
match self.get() {
Some(value) => OnceCell::from(value.clone()),
None => OnceCell::new(),
}
res
}
}