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