Trait AsyncFrom

Source
pub trait AsyncFrom<T>: Sized {
    // Required method
    fn async_from<'async_trait>(
        value: T,
    ) -> Pin<Box<dyn Future<Output = Self> + Send + 'async_trait>>
       where Self: 'async_trait;
}
Expand description

Trait for asynchronous conversions from a type T.

This trait allows for conversions that occur asynchronously, returning a Future.

§Example

use async_from::{ async_trait, AsyncFrom };

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

Required Methods§

Source

fn async_from<'async_trait>( value: T, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'async_trait>>
where Self: 'async_trait,

Asynchronously converts a value of type T into Self.

§Arguments
  • value - The value to be converted.
§Returns
  • Self - The converted value.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§