rstmt_traits/ops/
transform.rs

1/*
2    Appellation: transform <module>
3    Created At: 2025.12.24:14:22:11
4    Contrib: @FL03
5*/
6
7/// The [`Transform`] trait establishes a common interface for objects that can be transformed
8/// with respect to a given transformation, input, etc. to produce a new output.
9pub trait Transform<Rhs> {
10    type Output;
11
12    fn transform(&self, rhs: Rhs) -> Self::Output;
13}
14/// [`TryTransform`] defines a fallible transformation operation that can fail, producing an
15pub trait TryTransform<Rhs> {
16    type Output;
17    type Error;
18
19    fn try_transform(&self, rhs: Rhs) -> Result<Self::Output, Self::Error>;
20}
21
22/*
23 ************* Implementations *************
24*/
25
26impl<X, Y, A> TryTransform<X> for A
27where
28    A: Transform<X, Output = Y>,
29{
30    type Output = Y;
31    type Error = core::convert::Infallible;
32
33    fn try_transform(&self, rhs: X) -> Result<Y, Self::Error> {
34        Ok(<A as Transform<X>>::transform(self, rhs))
35    }
36}