Crates.io | async_from |
lib.rs | async_from |
version | 0.2.0 |
source | src |
created_at | 2024-11-05 07:25:10.979948 |
updated_at | 2024-11-05 07:36:04.594829 |
description | Async version of From, Into, TryFrom, TryInto. |
homepage | https://github.com/Wandalen/wTools/tree/master/module/core/async_from |
repository | https://github.com/Wandalen/wTools/tree/master/module/core/async_from |
max_upload_size | |
id | 1436171 |
size | 19,215 |
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.
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:
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.
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;
println!( "Converted: {}", num.0 );
let num : MyNumber = "42".to_string().async_into().await;
println!( "Converted: {}", num.0 );
}
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.
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()
{
match MyNumber::async_try_from( "42".to_string() ).await
{
Ok( my_num ) => println!( "Converted successfully: {}", my_num.0 ),
Err( e ) => println!( "Conversion failed: {:?}", e ),
}
let result : Result< MyNumber, _ > = "42".to_string().async_try_into().await;
match result
{
Ok( my_num ) => println!( "Converted successfully using AsyncTryInto: {}", my_num.0 ),
Err( e ) => println!( "Conversion failed using AsyncTryInto: {:?}", e ),
}
}