aboutsummaryrefslogtreecommitdiff
path: root/examples/async/animals/human.rs
blob: 5bc4903255d371d999e6ff8a1333604ff9fe6b86 (plain)
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
use std::time::Duration;

use syrette::injectable;
use syrette::ptr::{ThreadsafeSingletonPtr, TransientPtr};
use tokio::time::sleep;

use crate::interfaces::cat::ICat;
use crate::interfaces::dog::IDog;
use crate::interfaces::human::IHuman;

pub struct Human
{
    dog: ThreadsafeSingletonPtr<dyn IDog>,
    cat: TransientPtr<dyn ICat>,
}

#[injectable(IHuman, async = true)]
impl Human
{
    pub async fn new(
        dog: ThreadsafeSingletonPtr<dyn IDog>,
        cat: TransientPtr<dyn ICat>,
    ) -> Self
    {
        // The human needs some rest first
        sleep(Duration::from_secs(1)).await;

        Self { dog, cat }
    }
}

impl IHuman for Human
{
    fn make_pets_make_sounds(&self)
    {
        println!("Hi doggy!");

        self.dog.woof();

        println!("Hi kitty!");

        self.cat.meow();
    }
}