Reuse existing Somes in Option::(x)or

LLVM still has trouble re-using discriminants sometimes when rebuilding a two-variant enum, so when we have the correct variant already built, just use it.

That's simpler in LLVM *and* in MIR, so might as well: <https://rust.godbolt.org/z/KhdE8eToW>
This commit is contained in:
Scott McMurray 2023-10-06 01:41:48 -07:00
parent 579be69de9
commit 5432d13bb0

View File

@ -1475,7 +1475,7 @@ pub fn filter<P>(self, predicate: P) -> Self
#[stable(feature = "rust1", since = "1.0.0")]
pub fn or(self, optb: Option<T>) -> Option<T> {
match self {
Some(x) => Some(x),
x @ Some(_) => x,
None => optb,
}
}
@ -1500,7 +1500,7 @@ pub fn or_else<F>(self, f: F) -> Option<T>
F: FnOnce() -> Option<T>,
{
match self {
Some(x) => Some(x),
x @ Some(_) => x,
None => f(),
}
}
@ -1530,8 +1530,8 @@ pub fn or_else<F>(self, f: F) -> Option<T>
#[stable(feature = "option_xor", since = "1.37.0")]
pub fn xor(self, optb: Option<T>) -> Option<T> {
match (self, optb) {
(Some(a), None) => Some(a),
(None, Some(b)) => Some(b),
(a @ Some(_), None) => a,
(None, b @ Some(_)) => b,
_ => None,
}
}