basic_usage/
basic_usage.rs1mod basic_usage_example {
2 use async_trait::async_trait;
3 use async_try_from::{AsyncTryFrom, ValidateIntegrity, AsyncCreateWithAndValidate};
4 use std::io;
5
6 pub struct MyType;
8
9 #[async_trait]
11 impl AsyncTryFrom<String> for MyType {
12 type Error = io::Error;
13
14 async fn new(input: &String) -> Result<Self, Self::Error> {
15 if input.is_empty() {
16 Err(io::Error::new(io::ErrorKind::Other, "Input string is empty."))
17 } else {
18 Ok(MyType)
19 }
20 }
21 }
22
23 #[async_trait]
25 impl ValidateIntegrity for MyType {
26 type Error = io::Error;
27
28 async fn validate_integrity(&self) -> Result<(), Self::Error> {
29 Ok(())
31 }
32 }
33
34 pub async fn run_example() -> Result<(), Box<dyn std::error::Error>> {
36 let input = "Some input".to_string();
37 let _instance = MyType::new_and_validate(&input).await?;
38 println!("Successfully created and validated MyType instance.");
39 Ok(())
40 }
41}
42
43#[tokio::main]
44pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
45 println!("Run 'cargo run --example basic_usage' to see the async creation and validation in action.");
48 Ok(basic_usage_example::run_example().await?)
49}