use super::*;
#[ tokio::test ]
async fn async_try_from_test()
{
struct MyNumber( u32 );
#[ the_module::async_trait ]
impl the_module::AsyncTryFrom< String > for MyNumber
{
type Error = std::num::ParseIntError;
async fn async_try_from( value : String ) -> Result< Self, Self::Error >
{
tokio::time::sleep( tokio::time::Duration::from_millis( 10 ) ).await;
let num = value.parse::< u32 >()?;
Ok( MyNumber( num ) )
}
}
use the_module::{ AsyncTryFrom, AsyncTryInto };
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 ),
}
}
#[ tokio::test ]
async fn async_from_test()
{
struct MyNumber( u32 );
#[ the_module::async_trait ]
impl the_module::AsyncFrom< String > for MyNumber
{
async fn async_from( value : String ) -> Self
{
tokio::time::sleep( tokio::time::Duration::from_millis( 10 ) ).await;
let num = value.parse::< u32 >().unwrap_or( 0 );
MyNumber( num )
}
}
use the_module::{ AsyncFrom, AsyncInto };
let my_num : MyNumber = MyNumber::async_from( "42".to_string() ).await;
println!( "Converted successfully using AsyncFrom: {}", my_num.0 );
let my_num : MyNumber = "42".to_string().async_into().await;
println!( "Converted successfully using AsyncInto: {}", my_num.0 );
}