From 3dcb61021f7c665d2de8c1221ac3e913fe9d140b Mon Sep 17 00:00:00 2001 From: pantonshire Date: Sun, 5 Nov 2023 07:57:01 +0000 Subject: [PATCH] apply trait --- src/convert.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/convert.rs b/src/convert.rs index 6bafa4a..8bfd481 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -1,5 +1,25 @@ use core::convert::Infallible; +pub trait Apply: Sized { + fn apply(self, f: F) -> U + where + F: FnOnce(Self) -> U; +} + +impl Apply for T +where + T: Sized, +{ + #[inline] + #[must_use] + fn apply(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 /// 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