pth/
transitive.rs

1/// Define a private namespace for all its items.
2mod private
3{
4  // xxx: move to derive_tools
5
6  // qqq: write tests, lool into example
7  //
8  // impl< Initial > TransitiveTryFrom< AbsolutePath, PathError, Initial >
9  // for CrateDir
10  // where
11  //   AbsolutePath: TryFrom< Initial >,
12  //   PathError: From< < AbsolutePath as TryFrom< Initial > > ::Error >,
13  // {
14  // }
15
16  // qqq: implement transitive_from
17  // qqq: implement transitive_into
18
19  // qqq: move to derive_tools
20  // qqq: write tests, look into example
21  //
22  // impl< Initial > TransitiveTryFrom< AbsolutePath, PathError, Initial >
23  // for CrateDir
24  // where
25  //   AbsolutePath: TryFrom< Initial >,
26  //   PathError: From< < AbsolutePath as TryFrom< Initial > > ::Error >,
27  // {
28  // }
29  // qqq: implement transitive_try_into
30  // qqq: implement transitive_from
31  // qqq: implement transitive_into
32
33  /// A trait to perform a transitive `try_from` conversion.
34  ///
35  /// This trait allows for a two-step conversion process where an initial type `Initial`
36  /// is first converted to an intermediate type `Transitive`, and then to the final type `Self`.
37  ///
38  /// # Type Parameters
39  ///
40  /// - `Error` : The error type that can be produced during the conversion.
41  /// - `Initial` : The initial type from which the conversion starts.
42  ///
43  /// # Requirements
44  ///
45  /// - `Transitive` must implement `TryFrom< Initial >`.
46  /// - `Self` must implement `TryFrom< Transitive >` with the same error type.
47  /// - `Error` must implement `From< <Transitive as TryFrom<Initial >> ::Error>`.
48  ///
49  /// # Example
50  ///
51  /// ```rust
52  /// use pth ::TransitiveTryFrom;
53  /// use std ::convert ::TryFrom;
54  ///
55  /// struct InitialType;
56  /// struct IntermediateType;
57  /// struct FinalType;
58  /// struct ConversionError;
59  ///
60  /// impl TryFrom< InitialType > for IntermediateType
61  /// {
62  ///   type Error = ConversionError;
63  ///   fn try_from( value: InitialType ) -> Result< Self, Self ::Error >
64  ///   {
65  ///     // Conversion logic here
66  ///     Ok( IntermediateType )
67  /// }
68  /// }
69  ///
70  /// impl TryFrom< IntermediateType > for FinalType
71  /// {
72  ///   type Error = ConversionError;
73  ///   fn try_from( value: IntermediateType ) -> Result< Self, Self ::Error >
74  ///   {
75  ///     // Conversion logic here
76  ///     Ok( FinalType )
77  /// }
78  /// }
79  ///
80  /// let initial = InitialType;
81  /// let final_result: Result< FinalType, ConversionError > = FinalType ::transitive_try_from :: < IntermediateType >( initial );
82  /// ```
83  pub trait TransitiveTryFrom< Error, Initial >
84  {
85  /// Performs a transitive `try_from` conversion.
86  ///
87  /// This method first converts the `src` of type `Initial` to the intermediate type `Transitive`,
88  /// and then converts the intermediate type to the final type `Self`.
89  ///
90  /// # Arguments
91  ///
92  /// - `src` : The initial value to be converted.
93  ///
94  /// # Returns
95  ///
96  /// - `Ok(Self)` : If both conversions succeed.
97  /// - `Err(Error)` : If either conversion fails.
98  ///
99  /// # Example
100  ///
101  /// See the trait-level documentation for an example.
102  ///
103  /// # Errors
104  /// qqq: doc
105  #[ inline( always ) ]
106  fn transitive_try_from< Transitive >( src: Initial ) -> Result< Self, Error >
107  where
108   Transitive: TryFrom< Initial >,
109   Self: TryFrom< Transitive, Error = Error >,
110   Error: From< < Transitive as TryFrom< Initial > > ::Error >,
111  {
112   let src2 = TryFrom :: < Initial > ::try_from( src )?;
113   TryFrom :: < Transitive > ::try_from( src2 )
114 }
115 }
116
117  impl< Initial, Error, Final > TransitiveTryFrom< Error, Initial > for Final {}
118
119  /// A trait to perform a transitive `try_into` conversion.
120  ///
121  /// This trait allows for a two-step conversion process where an initial type `Self`
122  /// is first converted to an intermediate type `Transitive`, and then to the final type `Final`.
123  ///
124  /// # Type Parameters
125  ///
126  /// - `Error` : The error type that can be produced during the conversion.
127  /// - `Final` : The final type to which `Transitive` is converted.
128  ///
129  /// # Requirements
130  ///
131  /// - `Self` must implement `TryInto< Transitive >`.
132  /// - `Transitive` must implement `TryInto< Final >` with the same error type.
133  /// - `Error` must implement `From< <Self as TryInto<Transitive >> ::Error>`.
134  ///
135  /// # Example
136  ///
137  /// ```rust
138  /// use pth ::TransitiveTryInto;
139  /// use std ::convert ::TryInto;
140  ///
141  /// struct InitialType;
142  /// struct IntermediateType;
143  /// struct FinalType;
144  /// struct ConversionError;
145  ///
146  /// impl TryInto< IntermediateType > for InitialType
147  /// {
148  ///   type Error = ConversionError;
149  ///   fn try_into( self ) -> Result< IntermediateType, Self ::Error >
150  ///   {
151  ///     // Conversion logic here
152  ///     Ok( IntermediateType )
153  /// }
154  /// }
155  ///
156  /// impl TryInto< FinalType > for IntermediateType
157  /// {
158  ///   type Error = ConversionError;
159  ///   fn try_into( self ) -> Result< FinalType, Self ::Error >
160  ///   {
161  ///     // Conversion logic here
162  ///     Ok( FinalType )
163  /// }
164  /// }
165  ///
166  /// let initial = InitialType;
167  /// let final_result: Result< FinalType, ConversionError > = initial.transitive_try_into :: < IntermediateType >();
168  /// ```
169  pub trait TransitiveTryInto< Error, Final > : Sized
170  {
171  /// Performs a transitive `try_into` conversion.
172  ///
173  /// This method first converts `self` to the intermediate type `Transitive`,
174  /// and then converts the intermediate type to the final type `Final`.
175  ///
176  /// # Returns
177  ///
178  /// - `Ok(Final)` : If both conversions succeed.
179  /// - `Err(Error)` : If either conversion fails.
180  ///
181  /// # Example
182  ///
183  /// See the trait-level documentation for an example.
184  /// # Errors
185  /// qqq: doc
186  #[ inline( always ) ]
187  fn transitive_try_into< Transitive >( self ) -> Result< Final, Error >
188  where
189   Self: TryInto< Transitive >,
190   Transitive: TryInto< Final, Error = Error >,
191   Error: From< < Self as TryInto< Transitive > > ::Error >,
192  {
193   let src2 = TryInto :: < Transitive > ::try_into( self )?;
194   TryInto :: < Final > ::try_into( src2 )
195 }
196 }
197
198  impl< Error, Final, Initial > TransitiveTryInto< Error, Final > for Initial {}
199
200}
201
202crate ::mod_interface!
203{
204  exposed use TransitiveTryFrom;
205  exposed use TransitiveTryInto;
206}