diff options
| author | 2025-02-11 10:36:45 +0100 | |
|---|---|---|
| committer | 2025-02-11 10:36:45 +0100 | |
| commit | eab723866e1dc94ebd6d7c5c0c3ef191c80bcf59 (patch) | |
| tree | 2b5a476f9da5155fd13e68957a9bbf3e3ab352a1 /core | |
| parent | 080db348495f6e92f13391d3b222c5a2769b5fc1 (diff) | |
| download | iced-eab723866e1dc94ebd6d7c5c0c3ef191c80bcf59.tar.gz iced-eab723866e1dc94ebd6d7c5c0c3ef191c80bcf59.tar.bz2 iced-eab723866e1dc94ebd6d7c5c0c3ef191c80bcf59.zip | |
Replace `with` function with `Function` trait
Diffstat (limited to '')
| -rw-r--r-- | core/src/lib.rs | 107 | 
1 files changed, 52 insertions, 55 deletions
| diff --git a/core/src/lib.rs b/core/src/lib.rs index a5540787..cd05cb25 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -94,62 +94,59 @@ pub fn never<T>(never: std::convert::Infallible) -> T {      match never {}  } -/// Applies the given prefix value to the provided closure and returns -/// a new closure that takes the other argument. +/// A trait extension for binary functions (`Fn(A, B) -> O`).  /// -/// This lets you partially "apply" a function—equivalent to currying, -/// but it only works with binary functions. If you want to apply an -/// arbitrary number of arguments, use the [`with!`] macro instead. -/// -/// # When is this useful? -/// Sometimes you will want to identify the source or target -/// of some message in your user interface. This can be achieved through -/// normal means by defining a closure and moving the identifier -/// inside: -/// -/// ```rust -/// # let element: Option<()> = Some(()); -/// # enum Message { ButtonPressed(u32, ()) } -/// let id = 123; -/// -/// # let _ = { -/// element.map(move |result| Message::ButtonPressed(id, result)) -/// # }; -/// ``` -/// -/// That's quite a mouthful. [`with()`] lets you write: -/// -/// ```rust -/// # use iced_core::with; -/// # let element: Option<()> = Some(()); -/// # enum Message { ButtonPressed(u32, ()) } -/// let id = 123; -/// -/// # let _ = { -/// element.map(with(Message::ButtonPressed, id)) -/// # }; -/// ``` -/// -/// Effectively creating the same closure that partially applies -/// the `id` to the message—but much more concise! -pub fn with<T, R, O>( -    mut f: impl FnMut(T, R) -> O, -    prefix: T, -) -> impl FnMut(R) -> O -where -    T: Copy, -{ -    move |result| f(prefix, result) +/// It enables you to use a bunch of nifty functional programming paradigms +/// that work well with iced. +pub trait Function<A, B, O> { +    /// Applies the given first argument to a binary function and returns +    /// a new function that takes the other argument. +    /// +    /// This lets you partially "apply" a function—equivalent to currying, +    /// but it only works with binary functions. If you want to apply an +    /// arbitrary number of arguments, create a little struct for them. +    /// +    /// # When is this useful? +    /// Sometimes you will want to identify the source or target +    /// of some message in your user interface. This can be achieved through +    /// normal means by defining a closure and moving the identifier +    /// inside: +    /// +    /// ```rust +    /// # let element: Option<()> = Some(()); +    /// # enum Message { ButtonPressed(u32, ()) } +    /// let id = 123; +    /// +    /// # let _ = { +    /// element.map(move |result| Message::ButtonPressed(id, result)) +    /// # }; +    /// ``` +    /// +    /// That's quite a mouthful. [`with`] lets you write: +    /// +    /// ```rust +    /// # use iced_core::Function; +    /// # let element: Option<()> = Some(()); +    /// # enum Message { ButtonPressed(u32, ()) } +    /// let id = 123; +    /// +    /// # let _ = { +    /// element.map(Message::ButtonPressed.with(id)) +    /// # }; +    /// ``` +    /// +    /// Effectively creating the same closure that partially applies +    /// the `id` to the message—but much more concise! +    fn with(self, prefix: A) -> impl Fn(B) -> O;  } -/// Applies the given prefix values to the provided closure in the first -/// argument and returns a new closure that takes its last argument. -/// -/// This is a variadic version of [`with()`] which works with any number of -/// arguments. -#[macro_export] -macro_rules! with { -    ($f:expr, $($x:expr),+ $(,)?) => { -        move |result| $f($($x),+, result) -    }; +impl<F, A, B, O> Function<A, B, O> for F +where +    F: Fn(A, B) -> O, +    Self: Sized, +    A: Copy, +{ +    fn with(self, prefix: A) -> impl Fn(B) -> O { +        move |result| self(prefix, result) +    }  } | 
