rspace_traits/ops/
apply.rs

1/*
2    Appellation: apply <module>
3    Created At: 2025.12.24:17:20:29
4    Contrib: @FL03
5*/
6/// [`Apply`] establishes a binary operator for applying a given object, typically a function or
7/// closure, onto a target object, producing some output.
8pub trait Apply<Rhs> {
9    type Output;
10
11    fn apply(&self, rhs: Rhs) -> Self::Output;
12}
13/// The [`ApplyOnce`] trait defines an interface for objects capable of consuming themselves
14/// to apply the given function onto themselves or their elements to produce some output.
15pub trait ApplyOnce<Rhs> {
16    type Output;
17
18    fn apply_once(self, rhs: Rhs) -> Self::Output;
19}
20/// The [`ApplyMut`] trait defines an interface for objects capable of mutably borrowing themselves
21/// to apply the given function onto themselves or their elements to produce some output.
22pub trait ApplyMut<Rhs> {
23    type Output;
24
25    fn apply_mut(&mut self, rhs: Rhs) -> Self::Output;
26}
27
28pub trait TryApply<Rhs> {
29    type Output;
30    type Error;
31
32    fn try_apply(&self, rhs: Rhs) -> Result<Self::Output, Self::Error>;
33}
34
35pub trait TryApplyOnce<Rhs> {
36    type Output;
37    type Error;
38
39    fn try_apply_once(self, rhs: Rhs) -> Result<Self::Output, Self::Error>;
40}
41
42pub trait TryApplyMut<Rhs> {
43    type Output;
44    type Error;
45
46    fn try_apply_mut(&mut self, rhs: Rhs) -> Result<Self::Output, Self::Error>;
47}
48
49/*
50 ************* Implementations *************
51*/
52impl<A, X, Y> TryApply<X> for A
53where
54    A: Apply<X, Output = Y>,
55{
56    type Output = A::Output;
57    type Error = core::convert::Infallible;
58
59    fn try_apply(&self, rhs: X) -> Result<Self::Output, Self::Error> {
60        Ok(self.apply(rhs))
61    }
62}
63
64impl<A, X, Y> TryApplyOnce<X> for A
65where
66    A: ApplyOnce<X, Output = Y>,
67{
68    type Output = A::Output;
69    type Error = core::convert::Infallible;
70
71    fn try_apply_once(self, rhs: X) -> Result<Self::Output, Self::Error> {
72        Ok(self.apply_once(rhs))
73    }
74}
75
76impl<A, X, Y> TryApplyMut<X> for A
77where
78    A: ApplyMut<X, Output = Y>,
79{
80    type Output = A::Output;
81    type Error = core::convert::Infallible;
82
83    fn try_apply_mut(&mut self, rhs: X) -> Result<Self::Output, Self::Error> {
84        Ok(self.apply_mut(rhs))
85    }
86}