Introduce when_else()

This commit is contained in:
Joseph T. Lyons 2023-12-06 22:34:14 -05:00
parent 256f0308ae
commit 8fc15c05c5

View file

@ -69,6 +69,24 @@ pub trait IntoElement: Sized {
self.map(|this| if condition { then(this) } else { this }) self.map(|this| if condition { then(this) } else { this })
} }
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)
}
})
}
fn when_some<T>(self, option: Option<T>, then: impl FnOnce(Self, T) -> Self) -> Self fn when_some<T>(self, option: Option<T>, then: impl FnOnce(Self, T) -> Self) -> Self
where where
Self: Sized, Self: Sized,