# Module :: async_from
<!--{ generate.module_header.start() }-->
[](https://github.com/emersion/stability-badges#experimental) [](https://github.com/Wandalen/wTools/actions/workflows/workspace_push.yml) [](https://docs.rs/async_from) [](https://discord.gg/m3YfbXpUUY)
<!--{ generate.module_header.end }-->
Async version of From, Into, TryFrom, TryInto.
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.
## Why Asynchronous Conversion Traits?
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:
- **Simplicity**: Allow straightforward conversions without boilerplate, even in asynchronous contexts.
- **Consistency**: Provide a uniform interface for conversions across different types, aiding in writing predictable and maintainable code.
- **Error Handling**: Enable safe and explicit handling of conversion failures, essential for robust error management in commercial applications.
- **Asynchronous Contexts**: Facilitate conversions involving asynchronous operations, such as network requests or database queries, which are common in modern applications.
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.
### `AsyncFrom` and `AsyncInto`
Trait for asynchronous conversions from a type T.
These traits are designed for infallible asynchronous conversions. They allow you to convert types asynchronously, returning the result directly.
```rust
use async_from::{ async_trait, AsyncFrom, AsyncInto };
struct MyNumber( u32 );
#[ async_trait ]
impl AsyncFrom< String > for MyNumber
{
async fn async_from( value : String ) -> Self
{
let num = value.parse::< u32 >().unwrap_or( 0 );
MyNumber( num )
}
}
#[ tokio::main ]
async fn main()
{
let num = MyNumber::async_from( "42".to_string() ).await;
assert_eq!( num.0, 42, "AsyncFrom should convert '42' to 42" );
let num : MyNumber = "42".to_string().async_into().await;
assert_eq!( num.0, 42, "AsyncInto should convert '42' to 42" );
}
```
### `AsyncTryFrom` and `AsyncTryInto`
Trait for asynchronous fallible conversions from a type T.
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.
```rust
use async_from::{ async_trait, AsyncTryFrom, AsyncTryInto };
use std::num::ParseIntError;
struct MyNumber( u32 );
#[ async_trait ]
impl AsyncTryFrom< String > for MyNumber
{
type Error = ParseIntError;
async fn async_try_from( value : String ) -> Result< Self, Self::Error >
{
let num = value.parse::< u32 >()?;
Ok( MyNumber( num ) )
}
}
#[ tokio::main ]
async fn main()
{
let result = MyNumber::async_try_from( "42".to_string() ).await;
assert!( result.is_ok(), "AsyncTryFrom should succeed for valid input" );
assert_eq!( result.unwrap().0, 42, "AsyncTryFrom should convert '42' to 42" );
let result : Result< MyNumber, _ > = "42".to_string().async_try_into().await;
assert!( result.is_ok(), "AsyncTryInto should succeed for valid input" );
assert_eq!( result.unwrap().0, 42, "AsyncTryInto should convert '42' to 42" );
}
```