apply trait

This commit is contained in:
pantonshire committed 2023-11-05 07:57:01 +00:00
1 parent fc6caf0de6
commit 3dcb61021f
1 file changed
+20
+20
View File
@@ -1,5 +1,25 @@
use core::convert::Infallible; use core::convert::Infallible;
pub trait Apply: Sized {
fn apply<U, F>(self, f: F) -> U
where
F: FnOnce(Self) -> U;
}
impl<T> Apply for T
where
T: Sized,
{
#[inline]
#[must_use]
fn apply<U, F>(self, f: F) -> U
where
F: FnOnce(Self) -> U
{
f(self)
}
}
/// Consumes an element of type `Infallible` and produces an element of any type `T`. This is /// Consumes an element of type `Infallible` and produces an element of any type `T`. This is
/// possible because `Infallible` has no elements and thus this function can never be called, so /// possible because `Infallible` has no elements and thus this function can never be called, so
/// the statement "for any given type `T`, whenever this function returns, it returns an element of /// the statement "for any given type `T`, whenever this function returns, it returns an element of