pi_handler/
lib.rs

1extern crate fnv;
2extern crate pi_atom;
3extern crate pi_gray;
4
5use std::rc::Rc;
6use std::any::Any;
7use std::sync::Arc;
8use std::collections::HashMap;
9
10use futures::future::LocalBoxFuture;
11use fnv::FnvHashMap;
12
13use pi_atom::Atom;
14use pi_gray::GrayVersion;
15/*
16* 环境
17*/
18pub trait Env {
19    //获取属性
20    fn get_attr(&self, _key: Atom) -> Option<GenType> {
21        None
22    }
23
24    //设置属性,返回上个属性值
25    fn set_attr(&self, _key: Atom, _value: GenType) -> Option<GenType> {
26        None
27    }
28
29    //移除属性,返回属性值
30    fn remove_attr(&self, _key: Atom) -> Option<GenType> {
31        None
32    }
33}
34
35/*
36* 通用处理器
37*/
38pub trait Handler: Send + Sync {
39    type A;
40    type B;
41    type C;
42    type D;
43    type E;
44    type F;
45    type G;
46    type H;
47    type HandleResult;
48
49    //处理方法
50    fn handle(&self, env: Arc<dyn GrayVersion>, func: Atom, args: Args<Self::A, Self::B, Self::C, Self::D, Self::E, Self::F, Self::G, Self::H>) -> LocalBoxFuture<'static, Self::HandleResult>;
51}
52
53/*
54* 通用Map属性值
55*/
56#[derive(Debug, Clone)]
57pub enum GenMapType {
58    U8KeyMap(FnvHashMap<u8, GenType>),
59    U16KeyMap(FnvHashMap<u16, GenType>),
60    U32KeyMap(FnvHashMap<u32, GenType>),
61    U64KeyMap(FnvHashMap<u64, GenType>),
62    U128KeyMap(FnvHashMap<u128, GenType>),
63    USizeKeyMap(FnvHashMap<usize, GenType>),
64    I8KeyMap(FnvHashMap<i8, GenType>),
65    I16KeyMap(FnvHashMap<i16, GenType>),
66    I32KeyMap(FnvHashMap<i32, GenType>),
67    I64KeyMap(FnvHashMap<i64, GenType>),
68    I128KeyMap(FnvHashMap<i128, GenType>),
69    ISizeKeyMap(FnvHashMap<isize, GenType>),
70    StrKeyMap(HashMap<String, GenType>),
71    StringKeyMap(HashMap<Atom, GenType>),
72    BinKeyMap(HashMap<Vec<u8>, GenType>),
73    PtrKeyMap(FnvHashMap<*const dyn Any, GenType>),
74}
75
76/*
77* 通用属性值
78*/
79#[derive(Debug, Clone)]
80pub enum GenType {
81    Nil,
82    Bool(bool),
83    U8(u8),
84    U16(u16),
85    U32(u32),
86    U64(u64),
87    U128(u128),
88    USize(usize),
89    I8(i8),
90    I16(i16),
91    I32(i32),
92    I64(i64),
93    I128(i128),
94    ISize(isize),
95    F32(f32),
96    F64(f64),
97    Str(String),
98    String(Atom),
99    Bin(Vec<u8>),
100    BoxBin(Box<Vec<u8>>),
101    RcBin(Rc<Vec<u8>>),
102    ArcBin(Arc<Vec<u8>>),
103    PtrBin(*const u8),
104    Pointer(*const dyn Any),
105    Array(Vec<GenType>),
106    Map(GenMapType),
107    Obj(HashMap<Atom, GenType>),
108}
109
110/*
111* 安全的通用Map属性值
112*/
113#[derive(Debug, Clone)]
114pub enum SGenMapType {
115    U8KeyMap(FnvHashMap<u8, SGenType>),
116    U16KeyMap(FnvHashMap<u16, SGenType>),
117    U32KeyMap(FnvHashMap<u32, SGenType>),
118    U64KeyMap(FnvHashMap<u64, SGenType>),
119    U128KeyMap(FnvHashMap<u128, SGenType>),
120    USizeKeyMap(FnvHashMap<usize, SGenType>),
121    I8KeyMap(FnvHashMap<i8, SGenType>),
122    I16KeyMap(FnvHashMap<i16, SGenType>),
123    I32KeyMap(FnvHashMap<i32, SGenType>),
124    I64KeyMap(FnvHashMap<i64, SGenType>),
125    I128KeyMap(FnvHashMap<i128, SGenType>),
126    ISizeKeyMap(FnvHashMap<isize, SGenType>),
127    StrKeyMap(HashMap<String, SGenType>),
128    StringKeyMap(HashMap<Atom, SGenType>),
129    BinKeyMap(HashMap<Vec<u8>, SGenType>),
130}
131
132/*
133* 安全的通用属性值
134*/
135#[derive(Debug, Clone)]
136pub enum SGenType {
137    Nil,
138    Bool(bool),
139    U8(u8),
140    U16(u16),
141    U32(u32),
142    U64(u64),
143    U128(u128),
144    USize(usize),
145    I8(i8),
146    I16(i16),
147    I32(i32),
148    I64(i64),
149    I128(i128),
150    ISize(isize),
151    F32(f32),
152    F64(f64),
153    Str(String),
154    String(Atom),
155    Bin(Vec<u8>),
156    BoxBin(Box<Vec<u8>>),
157    ArcBin(Arc<Vec<u8>>),
158    Array(Vec<SGenType>),
159    Map(SGenMapType),
160    Obj(HashMap<Atom, SGenType>),
161}
162
163/*
164* 通用参数列表
165*/
166pub enum Args<A, B, C, D, E, F, G, H> {
167    NilArgs,                            //无参
168    OneArgs(A),
169    TwoArgs(A, B),
170    ThreeArgs(A, B, C),
171    FourArgs(A, B, C, D),
172    FiveArgs(A, B, C, D, E),
173    SixArgs(A, B, C, D, E, F),
174    SevenArgs(A, B, C, D, E, F, G),
175    EightArgs(A, B, C, D, E, F, G, H),
176    VarArgs(Vec<Box<dyn Any>>),             //变长参数
177}
178
179impl<A, B, C, D, E, F, G, H> Args<A, B, C, D, E, F, G, H> {
180    //构建一个指定数量的参数对象
181    pub fn with(size: usize) -> Self {
182        if size == 0 {
183            return Args::NilArgs;
184        }
185        Args::VarArgs(Vec::with_capacity(size))
186    }
187
188    //获取参数列表中参数的数量
189    pub fn len(&self) -> usize {
190        match self {
191            Args::NilArgs => 0,
192            Args::OneArgs(_) => 1,
193            Args::TwoArgs(_, _) => 2,
194            Args::ThreeArgs(_, _, _) => 3,
195            Args::FourArgs(_, _, _, _) => 4,
196            Args::FiveArgs(_, _, _, _, _) => 5,
197            Args::SixArgs(_, _, _, _, _, _) => 6,
198            Args::SevenArgs(_, _, _, _, _, _, _) => 7,
199            Args::EightArgs(_, _, _, _, _, _, _, _) => 8,
200            Args::VarArgs(args) => args.len(),
201        }
202    }
203
204    //获取变长参数指定位置的参数的只读引用
205    pub fn get_ref<T: Any>(&self, index: usize) -> Option<&T> {
206        if index >= self.len() {
207            return None;
208        }
209        
210        match self {
211            Args::VarArgs(args) => args[index].downcast_ref(),
212            _ => None,
213        }
214    }
215
216    //获取变长参数指定位置的参数的可写引用
217    pub fn get_mut<T: Any>(&mut self, index: usize) -> Option<&mut T> {
218        if index >= self.len() {
219            return None;
220        }
221
222         match self {
223            Args::VarArgs(args) => args[index].downcast_mut(),
224            _ => None,
225         }
226    }
227
228    //设置变长参数指定位置的参数
229    pub fn set<T: Any>(&mut self, index: usize, arg: T) -> &mut Self {
230        if index >= self.len() {
231            return self;
232        }
233
234        match self {
235            Args::VarArgs(args) => args[index] = Box::new(arg) as Box<dyn Any>,
236            _ => (),
237        }
238        self
239    }
240
241    //在变长参数列表头取出一个指定类型的参数,返回参数
242    pub fn pop<T: Any>(&mut self) -> Option<T> {
243        match self {
244            Args::VarArgs(args) => {
245                match args.pop() {
246                    None => None,
247                    Some(any) => {
248                        match any.downcast::<T>() {
249                            Err(_) => None,
250                            Ok(arg) => Some(*arg),
251                        }
252                    },
253                }
254            },
255            _ => None,
256        }
257    }
258
259    //在变长参数列表尾加入一个指定类型的参数,返回参数列表
260    pub fn push<T: Any>(&mut self, arg: T) -> &mut Self {
261        match self {
262            Args::VarArgs(args) => args.push(Box::new(arg) as Box<dyn Any>),
263            _ => (),
264        }
265        self
266    }
267
268    //移除变长参数列表指定位置的参数,返回参数
269    pub fn remove<T: Any>(&mut self, index: usize) -> Option<T> {
270        if index >= self.len() {
271            return None;
272        }
273
274        match self {
275            Args::VarArgs(args) => {
276                 match args.remove(index).downcast::<T>() {
277                    Err(_) => None,
278                    Ok(arg) => Some(*arg),
279                }
280            },
281            _ => None,
282        }
283    }
284
285    //移除变长参数列表所有的参数, 返回参数数量
286    pub fn clear(&mut self) -> usize {
287        let len = self.len();
288        match self {
289            Args::VarArgs(args) => args.clear(),
290            _ => (),
291        }
292        len
293    }
294}