2 unstable releases

0.2.0 Nov 5, 2024
0.1.0 Nov 5, 2024

#1823 in Algorithms

Download history 254/week @ 2024-11-04 8/week @ 2024-11-11 21/week @ 2024-11-18

283 downloads per month
Used in async_tools

MIT license

16KB
113 lines

Module :: async_from

experimental rust-status docs.rs discord

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.

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 ),
  }
}

Dependencies

~235–680KB
~16K SLoC