basic_usage/
basic_usage.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
mod basic_usage_example {
    use async_trait::async_trait;
    use async_try_from::{AsyncTryFrom, ValidateIntegrity, AsyncCreateWithAndValidate};
    use std::io;

    // A simple struct that we will create asynchronously
    pub struct MyType;

    // Implement async creation from a String
    #[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)
            }
        }
    }

    // Implement a basic validation check
    impl ValidateIntegrity for MyType {
        type Error = io::Error;

        fn validate_integrity(&self) -> Result<(), Self::Error> {
            // Add real integrity checks here if needed
            Ok(())
        }
    }

    // Demonstrate creating and validating our type in one step
    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>> {
    // In a real project, you could call:
    // This example keeps main synchronous for demonstration.
    println!("Run 'cargo run --example basic_usage' to see the async creation and validation in action.");
    Ok(basic_usage_example::run_example().await?)
}