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:
parent
596ee58be8
commit
298ca5ff1b
6 changed files with 55 additions and 93 deletions
|
@ -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"))]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue