basic_usage/
basic_usage.rsmod basic_usage_example {
use async_trait::async_trait;
use async_try_from::{AsyncTryFrom, ValidateIntegrity, AsyncCreateWithAndValidate};
use std::io;
pub struct MyType;
#[async_trait]
impl AsyncTryFrom<String> for MyType {
type Error = io::Error;
async fn new(input: &String) -> Result<Self, Self::Error> {
if input.is_empty() {
Err(io::Error::new(io::ErrorKind::Other, "Input string is empty."))
} else {
Ok(MyType)
}
}
}
impl ValidateIntegrity for MyType {
type Error = io::Error;
fn validate_integrity(&self) -> Result<(), Self::Error> {
Ok(())
}
}
pub async fn run_example() -> Result<(), Box<dyn std::error::Error>> {
let input = "Some input".to_string();
let _instance = MyType::new_and_validate(&input).await?;
println!("Successfully created and validated MyType instance.");
Ok(())
}
}
#[tokio::main]
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Run 'cargo run --example basic_usage' to see the async creation and validation in action.");
Ok(basic_usage_example::run_example().await?)
}