async_from/
lib.rs

1
2#![ doc( html_logo_url = "https://raw.githubusercontent.com/Wandalen/wTools/master/asset/img/logo_v3_trans_square.png" ) ]
3#![ doc( html_favicon_url = "https://raw.githubusercontent.com/Wandalen/wTools/alpha/asset/img/logo_v3_trans_square_icon_small_v2.ico" ) ]
4#![ doc( html_root_url = "https://docs.rs/async_from/latest/async_from/" ) ]
5#![ doc = include_str!( concat!( env!( "CARGO_MANIFEST_DIR" ), "/", "Readme.md" ) ) ]
6
7/// Namespace with dependencies.
8#[ cfg( feature = "enabled" ) ]
9pub mod dependency
10{
11  pub use ::async_trait;
12}
13
14/// Internal namespace.
15#[ cfg( feature = "enabled" ) ]
16mod private
17{
18
19  pub use async_trait::async_trait;
20  use std::fmt::Debug;
21
22  /// Trait for asynchronous conversions from a type `T`.
23  ///
24  /// This trait allows for conversions that occur asynchronously, returning a `Future`.
25  ///
26  /// # Example
27  ///
28  /// ```rust
29  /// use async_from::{ async_trait, AsyncFrom };
30  ///
31  /// struct MyNumber( u32 );
32  ///
33  /// #[ async_trait ]
34  /// impl AsyncFrom< String > for MyNumber
35  /// {
36  ///   async fn async_from( value : String ) -> Self
37  ///   {
38  ///     let num = value.parse::< u32 >().unwrap_or( 0 );
39  ///     MyNumber( num )
40  ///   }
41  /// }
42  ///
43  /// #[ tokio::main ]
44  /// async fn main()
45  /// {
46  ///   let num = MyNumber::async_from( "42".to_string() ).await;
47  ///   println!( "Converted: {}", num.0 );
48  /// }
49  /// ```
50  #[ cfg( feature = "async_from" ) ]
51  #[ async_trait ]
52  pub trait AsyncFrom< T > : Sized
53  {
54    /// Asynchronously converts a value of type `T` into `Self`.
55    ///
56    /// # Arguments
57    ///
58    /// * `value` - The value to be converted.
59    ///
60    /// # Returns
61    ///
62    /// * `Self` - The converted value.
63    async fn async_from( value : T ) -> Self;
64  }
65
66  /// Trait for asynchronous conversions into a type `T`.
67  ///
68  /// This trait provides a method to convert `Self` into `T` asynchronously.
69  ///
70  /// # Example
71  ///
72  /// ```rust
73  /// use async_from::{ async_trait, AsyncFrom, AsyncInto };
74  ///
75  /// struct MyNumber( u32 );
76  ///
77  /// #[ async_trait ]
78  /// impl AsyncFrom< String > for MyNumber
79  /// {
80  ///   async fn async_from( value : String ) -> Self
81  ///   {
82  ///     let num = value.parse::< u32 >().unwrap_or( 0 );
83  ///     MyNumber( num )
84  ///   }
85  /// }
86  ///
87  /// #[ tokio::main ]
88  /// async fn main()
89  /// {
90  ///   let num : MyNumber = "42".to_string().async_into().await;
91  ///   println!( "Converted: {}", num.0 );
92  /// }
93  /// ```
94  #[ async_trait ]
95  #[ cfg( feature = "async_from" ) ]
96  pub trait AsyncInto< T > : Sized
97  {
98    /// Asynchronously converts `Self` into a value of type `T`.
99    ///
100    /// # Returns
101    ///
102    /// * `T` - The converted value.
103    async fn async_into( self ) -> T;
104  }
105
106  /// Blanket implementation of `AsyncInto` for any type that implements `AsyncFrom`.
107  ///
108  /// This implementation allows any type `T` that implements `AsyncFrom<U>` to also implement `AsyncInto<U>`.
109  #[ async_trait ]
110  #[ cfg( feature = "async_from" ) ]
111  impl< T, U > AsyncInto< U > for T
112  where
113    U : AsyncFrom< T > + Send,
114    T : Send,
115  {
116    /// Asynchronously converts `Self` into a value of type `U` using `AsyncFrom`.
117    ///
118    /// # Returns
119    ///
120    /// * `U` - The converted value.
121    async fn async_into( self ) -> U
122    {
123      U::async_from( self ).await
124    }
125  }
126
127  /// Trait for asynchronous fallible conversions from a type `T`.
128  ///
129  /// This trait allows for conversions that may fail, returning a `Result` wrapped in a `Future`.
130  ///
131  /// # Example
132  ///
133  /// ```rust
134  /// use async_from::{ async_trait, AsyncTryFrom };
135  /// use std::num::ParseIntError;
136  ///
137  /// struct MyNumber( u32 );
138  ///
139  /// #[ async_trait ]
140  /// impl AsyncTryFrom< String > for MyNumber
141  /// {
142  ///   type Error = ParseIntError;
143  ///
144  ///   async fn async_try_from( value : String ) -> Result< Self, Self::Error >
145  ///   {
146  ///     let num = value.parse::< u32 >()?;
147  ///     Ok( MyNumber( num ) )
148  ///   }
149  /// }
150  ///
151  /// #[ tokio::main ]
152  /// async fn main()
153  /// {
154  ///   match MyNumber::async_try_from( "42".to_string() ).await
155  ///   {
156  ///     Ok( my_num ) => println!( "Converted successfully: {}", my_num.0 ),
157  ///     Err( e ) => println!( "Conversion failed: {:?}", e ),
158  ///   }
159  /// }
160  /// ```
161  #[ async_trait ]
162  #[ cfg( feature = "async_try_from" ) ]
163  pub trait AsyncTryFrom< T > : Sized
164  {
165    /// The error type returned if the conversion fails.
166    type Error : Debug;
167
168    /// Asynchronously attempts to convert a value of type `T` into `Self`.
169    ///
170    /// # Arguments
171    ///
172    /// * `value` - The value to be converted.
173    ///
174    /// # Returns
175    ///
176    /// * `Result<Self, Self::Error>` - On success, returns the converted value. On failure, returns an error.
177    async fn async_try_from( value : T ) -> Result< Self, Self::Error >;
178  }
179
180  /// Trait for asynchronous fallible conversions into a type `T`.
181  ///
182  /// This trait provides a method to convert `Self` into `T`, potentially returning an error.
183  ///
184  /// # Example
185  ///
186  /// ```rust
187  /// use async_from::{ async_trait, AsyncTryFrom, AsyncTryInto };
188  /// use std::num::ParseIntError;
189  ///
190  /// struct MyNumber( u32 );
191  ///
192  /// #[ async_trait ]
193  /// impl AsyncTryFrom< String > for MyNumber
194  /// {
195  ///   type Error = ParseIntError;
196  ///
197  ///   async fn async_try_from( value : String ) -> Result< Self, Self::Error >
198  ///   {
199  ///     let num = value.parse::< u32 >()?;
200  ///     Ok( MyNumber( num ) )
201  ///   }
202  /// }
203  ///
204  /// #[ tokio::main ]
205  /// async fn main()
206  /// {
207  ///   let result : Result< MyNumber, _ > = "42".to_string().async_try_into().await;
208  ///   match result
209  ///   {
210  ///     Ok( my_num ) => println!( "Converted successfully using AsyncTryInto: {}", my_num.0 ),
211  ///     Err( e ) => println!( "Conversion failed using AsyncTryInto: {:?}", e ),
212  ///   }
213  /// }
214  /// ```
215  #[ async_trait ]
216  #[ cfg( feature = "async_try_from" ) ]
217  pub trait AsyncTryInto< T > : Sized
218  {
219    /// The error type returned if the conversion fails.
220    type Error : Debug;
221
222    /// Asynchronously attempts to convert `Self` into a value of type `T`.
223    ///
224    /// # Returns
225    ///
226    /// * `Result<T, Self::Error>` - On success, returns the converted value. On failure, returns an error.
227    async fn async_try_into( self ) -> Result< T, Self::Error >;
228  }
229
230  /// Blanket implementation of `AsyncTryInto` for any type that implements `AsyncTryFrom`.
231  ///
232  /// This implementation allows any type `T` that implements `AsyncTryFrom<U>` to also implement `AsyncTryInto<U>`.
233  #[ async_trait ]
234  #[ cfg( feature = "async_try_from" ) ]
235  impl< T, U > AsyncTryInto< U > for T
236  where
237    U : AsyncTryFrom< T > + Send,
238    T : Send,
239  {
240    type Error = U::Error;
241
242    /// Asynchronously converts `Self` into a value of type `U` using `AsyncTryFrom`.
243    ///
244    /// # Returns
245    ///
246    /// * `Result<U, Self::Error>` - On success, returns the converted value. On failure, returns an error.
247    async fn async_try_into( self ) -> Result< U, Self::Error >
248    {
249      U::async_try_from( self ).await
250    }
251  }
252
253}
254
255#[ cfg( feature = "enabled" ) ]
256#[ doc( inline ) ]
257#[ allow( unused_imports ) ]
258pub use own::*;
259
260/// Own namespace of the module.
261#[ cfg( feature = "enabled" ) ]
262#[ allow( unused_imports ) ]
263pub mod own
264{
265  use super::*;
266  #[ doc( inline ) ]
267  pub use orphan::*;
268}
269
270/// Orphan namespace of the module.
271#[ cfg( feature = "enabled" ) ]
272#[ allow( unused_imports ) ]
273pub mod orphan
274{
275  use super::*;
276  #[ doc( inline ) ]
277  pub use exposed::*;
278
279}
280
281/// Exposed namespace of the module.
282#[ cfg( feature = "enabled" ) ]
283#[ allow( unused_imports ) ]
284pub mod exposed
285{
286  use super::*;
287
288  #[ doc( inline ) ]
289  pub use prelude::*;
290
291  #[ doc( inline ) ]
292  pub use ::async_trait::async_trait;
293
294  #[ cfg( feature = "async_from" ) ]
295  pub use private::
296  {
297    AsyncFrom,
298    AsyncInto,
299  };
300
301  #[ cfg( feature = "async_try_from" ) ]
302  pub use private::
303  {
304    AsyncTryFrom,
305    AsyncTryInto,
306  };
307
308}
309
310/// Prelude to use essentials: `use my_module::prelude::*`.
311#[ cfg( feature = "enabled" ) ]
312#[ allow( unused_imports ) ]
313pub mod prelude
314{
315  use super::*;
316}