Prefer .map for conditionals with else conditions (#15118)

This PR updates instances where we were using `.when_else` and
`.when_else_some` to use `.map` with a conditional inside.

This allows us to avoid reinventing Rust's syntax for conditionals and
(IMO) makes the code easier to read.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-07-24 17:09:07 -04:00 committed by GitHub
parent 596ee58be8
commit 298ca5ff1b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 55 additions and 93 deletions

View file

@ -40,44 +40,6 @@ pub trait FluentBuilder {
}
})
}
/// Conditionally unwrap and modify self with one closure if the given option is Some, or another if it is None.
fn when_some_else<T>(
self,
option: Option<T>,
then: impl FnOnce(Self, T) -> Self,
otherwise: impl FnOnce(Self) -> Self,
) -> Self
where
Self: Sized,
{
self.map(|this| {
if let Some(value) = option {
then(this, value)
} else {
otherwise(this)
}
})
}
/// Conditionally modify self with one closure or another
fn when_else(
self,
condition: bool,
then: impl FnOnce(Self) -> Self,
otherwise: impl FnOnce(Self) -> Self,
) -> Self
where
Self: Sized,
{
self.map(|this| {
if condition {
then(this)
} else {
otherwise(this)
}
})
}
}
#[cfg(any(test, feature = "test-support"))]