timer_util/
traits.rs

1use anyhow::{bail, Result};
2use std::ops::{Bound, RangeBounds};
3
4pub trait Computer {
5    const MIN: u64;
6    type DataTy;
7
8    /// 下个循环的第一个符合值
9    fn update_to_next_ring(&mut self);
10
11    fn is_match(&self) -> bool;
12    // 因为结果可能用来赋值,因此用DataTy,可以避免Result。不包含index
13    fn next_val(&self) -> Option<Self::DataTy>;
14    fn min_val(&self) -> Self::DataTy;
15    fn val_mut(&mut self, val: Self::DataTy);
16    fn val(&self) -> u64;
17}
18/// 配置项的操作trait
19pub trait ConfigOperator: Sized {
20    /// 最小值:比如星期配置,则最小为星期1,即为1,即0b10,即u64的第1位为1
21    const MIN: u64;
22    /// 最大值:比如星期配置,则最大为星期日,即为7,即0b10000000,即u64的第7位为1
23    const MAX: u64;
24    /// 满值:即全选的值,比如星期7天全选,则为二进制1111 1110
25    const DEFAULT_MAX: u64;
26
27    type DataTy: AsBizData<u64> + Copy + Clone;
28
29    fn _default() -> Self;
30    #[inline]
31    fn default_value(val: Self::DataTy) -> Self {
32        let ins = Self::_default();
33        ins.add(val)
34    }
35    #[inline]
36    fn default_range(range: impl RangeBounds<Self::DataTy>) -> Result<Self> {
37        let ins = Self::_default();
38        ins.add_range(range)
39    }
40    #[inline]
41    fn default_all() -> Self {
42        let mut ins = Self::_default();
43        ins._val_mut(Self::DEFAULT_MAX);
44        ins
45    }
46    #[inline]
47    fn default_all_by_max(max: Self::DataTy) -> Self {
48        let mut ins = Self::_default();
49        let mut val = ins._val();
50        let mut index = Self::MIN;
51        while index <= max.as_data() {
52            val |= 1 << index.clone();
53            index += 1;
54        }
55        ins._val_mut(val);
56        ins
57    }
58    fn default_array(vals: &[Self::DataTy]) -> Self {
59        let ins = Self::_default();
60        ins.add_array(vals)
61    }
62    fn add_array(mut self, vals: &[Self::DataTy]) -> Self {
63        let mut val = self._val();
64        for i in vals {
65            val |= 1 << i.as_data();
66        }
67        self._val_mut(val);
68        self
69    }
70    fn add(mut self, index: Self::DataTy) -> Self {
71        let index = index.as_data();
72        self._val_mut(self._val() | (1 << index));
73        self
74    }
75    fn add_range(mut self, range: impl RangeBounds<Self::DataTy>) -> Result<Self> {
76        let mut first = match range.start_bound() {
77            Bound::Unbounded => Self::MIN,
78            Bound::Included(first) => first.as_data(),
79            Bound::Excluded(first) => first.as_data() + 1,
80        };
81        let end = match range.end_bound() {
82            Bound::Unbounded => Self::MAX,
83            Bound::Included(end) => end.as_data(),
84            Bound::Excluded(end) => end.as_data() - 1,
85        };
86        if first > end {
87            bail!("error:{} > {}", first, end);
88        }
89        let mut val = self._val();
90        while first <= end {
91            val |= 1 << first;
92            first += 1;
93        }
94        self._val_mut(val);
95        Ok(self)
96    }
97    /// 生成2者的并集
98    fn merge(&self, other: &Self) -> Self {
99        let mut new = Self::_default();
100        new._val_mut(self._val() | other._val());
101        new
102    }
103    /// 生成2者的交集
104    fn intersection(&self, other: &Self) -> Self {
105        let mut new = Self::_default();
106        new._val_mut(self._val() & other._val());
107        new
108    }
109
110    fn to_vec(&self) -> Vec<u64> {
111        let mut res = Vec::new();
112        let val = self._val();
113        let mut first = Self::MIN;
114        while first <= Self::MAX {
115            if (val & (1 << first)) > 0 {
116                res.push(first);
117            }
118            first += 1;
119        }
120        res
121    }
122    fn contain(&self, index: Self::DataTy) -> bool {
123        let index = index.as_data();
124        let val = self._val();
125        val & (1 << index) > 0
126    }
127    fn next(&self, index: Self::DataTy) -> Option<Self::DataTy>;
128    /// 取下一个持有值,不包括index
129    fn _next(&self, index: Self::DataTy) -> Option<u64> {
130        let mut first = index.as_data() + 1;
131        let val = self._val();
132        while first <= Self::MAX {
133            if (val & (1 << first)) > 0 {
134                return Some(first);
135            }
136            first += 1;
137        }
138        None
139    }
140    fn min_val(&self) -> Self::DataTy;
141    /// 取最小的持有值
142    fn _min_val(&self) -> u64 {
143        let mut first = Self::MIN;
144        let val = self._val();
145        while first <= Self::MAX {
146            if (val & (1 << first)) > 0 {
147                return first;
148            }
149            first += 1;
150        }
151        unreachable!("it is a bug");
152    }
153    fn _val(&self) -> u64;
154    fn _val_mut(&mut self, val: u64);
155
156    /// 是否啥都没有选
157    fn is_zero(&self) -> bool {
158        self._val() == 0
159    }
160}
161
162pub trait AsBizData<Ty>: Copy {
163    fn as_data(self) -> Ty;
164}
165
166pub trait FromData<Ty> {
167    fn from_data(val: Ty) -> Self;
168}
169
170pub trait TryFromData<Ty>: FromData<Ty> {
171    fn try_from_data(val: Ty) -> anyhow::Result<Self>
172    where
173        Self: Sized;
174}