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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
pub mod schedule {
    use std::time;
    use std::time::Duration;
    use std::ops::FnMut;
    use std::borrow::{Borrow, BorrowMut};
    use crate::errors::errors::TError;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicI64, AtomicI32};

    pub trait TaskAction : Send + Sync {
        fn execute(&self,id:u64) {}
        fn date_format(&self) -> &str;
        fn tick(&self) -> u64;
        fn id(&self) -> u64;
        fn loop_count(&self) -> i32;
    }

    struct TaskEmpty {}
    impl TaskAction for TaskEmpty {
        fn date_format(&self) -> &str {
            ""
        }

        fn tick(&self) -> u64 {
            0
        }

        fn id(&self) -> u64 {
            0
        }

        fn loop_count(&self) -> i32 {
            0
        }
    }

    // 内部实现的自己绑定自己函数的实现
    pub struct ClosuresAction {
        date_format: String,
        id:u64,
        loop_count:i32,
        tick:Duration,
        call:Arc<dyn Fn(u64) + Send + Sync + 'static>,
    }

    impl ClosuresAction {
        pub fn new(date:&str,idx:u64,loopC:i32,t:time::Duration,f: impl Fn(u64) + Send+Sync + 'static) -> ClosuresAction {
            ClosuresAction {
                date_format: String::from(date),
                id: idx,
                tick:t,
                loop_count: loopC,
                call:Arc::new(f),
            }
        }
    }

    // 实现这个trait
    impl TaskAction for ClosuresAction {
        fn execute(&self,id:u64) {
            (self.call)(id);
        }

        fn date_format(&self) -> &str {
            self.date_format.as_str()
        }
        fn tick(&self) -> u64 { self.tick.as_millis() as u64 }
        fn id(&self) -> u64 {
            self.id
        }
        fn loop_count(&self) -> i32 {
            self.loop_count
        }
    }
}