baichun_framework_core/
traits.rs1use async_trait::async_trait;
2
3use crate::error::Result;
4
5pub trait Convert<T> {
7 fn convert(&self) -> T;
9}
10
11pub trait CloneInto<T: Clone> {
13 fn clone_into(&self) -> T;
15}
16
17pub trait Initialize {
19 fn initialize(&mut self) -> Result<()>;
21}
22
23pub trait Validate {
25 fn validate(&self) -> Result<()>;
27}
28
29pub trait Lifecycle {
31 fn start(&mut self) -> Result<()>;
33 fn stop(&mut self) -> Result<()>;
35 fn restart(&mut self) -> Result<()>;
37}
38
39#[async_trait]
41pub trait AsyncLifecycle {
42 async fn start(&mut self) -> Result<()>;
44 async fn stop(&mut self) -> Result<()>;
46 async fn restart(&mut self) -> Result<()>;
48}
49
50pub trait Serializer {
52 fn to_bytes(&self) -> Result<Vec<u8>>;
54 fn from_bytes(bytes: &[u8]) -> Result<Self>
56 where
57 Self: Sized;
58}
59
60pub trait CloneBox {
62 fn clone_box(&self) -> Box<dyn CloneBox>;
64}
65
66impl<T> CloneBox for T
67where
68 T: 'static + Clone,
69{
70 fn clone_box(&self) -> Box<dyn CloneBox> {
71 Box::new(self.clone())
72 }
73}
74
75pub trait Identifier {
77 fn id(&self) -> String;
79}
80
81pub trait Named {
83 fn name(&self) -> String;
85}
86
87pub trait Described {
89 fn description(&self) -> String;
91}
92
93pub trait Versioned {
95 fn version(&self) -> String;
97}
98
99pub trait State {
101 fn state(&self) -> String;
103 fn is_available(&self) -> bool;
105}
106
107pub trait Tagged {
109 fn tags(&self) -> Vec<String>;
111 fn add_tag(&mut self, tag: String);
113 fn remove_tag(&mut self, tag: &str);
115 fn has_tag(&self, tag: &str) -> bool;
117}