|
| 1 | +<!-- {{# generate.module_header{} #}} --> |
| 2 | + |
| 3 | +# Module :: async_from |
| 4 | +[](https://github.com/emersion/stability-badges#experimental) [](https://github.com/Wandalen/wTools/actions/workflows/Moduleasync_fromPush.yml) [](https://docs.rs/async_from) [](https://discord.gg/m3YfbXpUUY) |
| 5 | + |
| 6 | +Async version of From, Into, TryFrom, TryInto. |
| 7 | + |
| 8 | +The `async_from` crate provides asynchronous versions of the well-known `From`, `Into`, `TryFrom`, and `TryInto` traits. These traits are essential for handling conversions in Rust, and their asynchronous counterparts, allowing for conversions that involve asynchronous operations. |
| 9 | + |
| 10 | +## Why Asynchronous Conversion Traits? |
| 11 | + |
| 12 | +In Rust, the `From`, `Into`, `TryFrom`, and `TryInto` traits provide a standardized way to handle type conversions. The `async_from` module extends this functionality to asynchronous contexts with `AsyncFrom`, `AsyncInto`, `AsyncTryFrom`, and `AsyncTryInto` traits, offering several key benefits: |
| 13 | + |
| 14 | +- **Simplicity**: Allow straightforward conversions without boilerplate, even in asynchronous contexts. |
| 15 | +- **Consistency**: Provide a uniform interface for conversions across different types, aiding in writing predictable and maintainable code. |
| 16 | +- **Error Handling**: Enable safe and explicit handling of conversion failures, essential for robust error management in commercial applications. |
| 17 | +- **Asynchronous Contexts**: Facilitate conversions involving asynchronous operations, such as network requests or database queries, which are common in modern applications. |
| 18 | + |
| 19 | +The `async_from` provides developers with the tools needed to handle complex conversions in an async context efficiently, which is particularly important for commercial applications requiring reliable and efficient handling of asynchronous operations. |
| 20 | + |
| 21 | +### `AsyncFrom` and `AsyncInto` |
| 22 | + |
| 23 | +Trait for asynchronous conversions from a type T. |
| 24 | + |
| 25 | +These traits are designed for infallible asynchronous conversions. They allow you to convert types asynchronously, returning the result directly. |
| 26 | + |
| 27 | +```rust |
| 28 | +use async_from::{ async_trait, AsyncFrom, AsyncInto }; |
| 29 | + |
| 30 | +struct MyNumber( u32 ); |
| 31 | + |
| 32 | +#[ async_trait ] |
| 33 | +impl AsyncFrom< String > for MyNumber |
| 34 | +{ |
| 35 | + async fn async_from( value : String ) -> Self |
| 36 | + { |
| 37 | + let num = value.parse::< u32 >().unwrap_or( 0 ); |
| 38 | + MyNumber( num ) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[ tokio::main ] |
| 43 | +async fn main() |
| 44 | +{ |
| 45 | + let num = MyNumber::async_from( "42".to_string() ).await; |
| 46 | + println!( "Converted: {}", num.0 ); |
| 47 | + let num : MyNumber = "42".to_string().async_into().await; |
| 48 | + println!( "Converted: {}", num.0 ); |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +### `AsyncTryFrom` and `AsyncTryInto` |
| 53 | + |
| 54 | +Trait for asynchronous fallible conversions from a type T. |
| 55 | + |
| 56 | +These traits are for fallible asynchronous conversions, where the conversion might fail. They return a `Result` wrapped in a `Future`, allowing you to handle errors gracefully. |
| 57 | + |
| 58 | +```rust |
| 59 | +use async_from::{ async_trait, AsyncTryFrom, AsyncTryInto }; |
| 60 | +use std::num::ParseIntError; |
| 61 | + |
| 62 | +struct MyNumber( u32 ); |
| 63 | + |
| 64 | +#[ async_trait ] |
| 65 | +impl AsyncTryFrom< String > for MyNumber |
| 66 | +{ |
| 67 | + type Error = ParseIntError; |
| 68 | + |
| 69 | + async fn async_try_from( value : String ) -> Result< Self, Self::Error > |
| 70 | + { |
| 71 | + let num = value.parse::< u32 >()?; |
| 72 | + Ok( MyNumber( num ) ) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +#[ tokio::main ] |
| 77 | +async fn main() |
| 78 | +{ |
| 79 | + match MyNumber::async_try_from( "42".to_string() ).await |
| 80 | + { |
| 81 | + Ok( my_num ) => println!( "Converted successfully: {}", my_num.0 ), |
| 82 | + Err( e ) => println!( "Conversion failed: {:?}", e ), |
| 83 | + } |
| 84 | + let result : Result< MyNumber, _ > = "42".to_string().async_try_into().await; |
| 85 | + match result |
| 86 | + { |
| 87 | + Ok( my_num ) => println!( "Converted successfully using AsyncTryInto: {}", my_num.0 ), |
| 88 | + Err( e ) => println!( "Conversion failed using AsyncTryInto: {:?}", e ), |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
0 commit comments