axion_data/
dtype.rs

1use std::cmp::Ordering;
2use std::fmt::Debug;
3use serde::{Deserialize, Serialize};
4
5/// 数据类型枚举
6/// 
7/// 定义了 Axion 库支持的所有数据类型,包括基础类型和复合类型。
8/// 支持序列化、反序列化以及类型比较和排序。
9/// 
10/// # 类型层次
11/// 
12/// - **Null** - 空值类型
13/// - **Bool** - 布尔类型
14/// - **整数类型** - Int8, Int16, Int32, Int64, Int128
15/// - **无符号整数类型** - UInt8, UInt16, UInt32, UInt64, UInt128
16/// - **浮点类型** - Float32, Float64
17/// - **字符串类型** - String
18/// - **复合类型** - List(内部类型)
19#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
20pub enum DataType {
21    /// 空值类型
22    Null,
23    /// 布尔类型
24    Bool,
25    /// 8位有符号整数
26    Int8,
27    /// 16位有符号整数
28    Int16,
29    /// 32位有符号整数
30    Int32,
31    /// 64位有符号整数
32    Int64,
33    /// 128位有符号整数
34    Int128,
35    /// 8位无符号整数
36    UInt8,
37    /// 16位无符号整数
38    UInt16,
39    /// 32位无符号整数
40    UInt32,
41    /// 64位无符号整数
42    UInt64,
43    /// 128位无符号整数
44    UInt128,
45    /// 32位浮点数
46    Float32,
47    /// 64位浮点数
48    Float64,
49    /// 字符串类型
50    String,
51    /// 列表类型,包含内部元素的数据类型
52    List(Box<DataType>),
53}
54
55impl DataType {
56    /// 检查数据类型是否为浮点类型
57    pub fn is_float(&self) -> bool {
58        matches!(self, DataType::Float32 | DataType::Float64)
59    }
60
61    /// 检查数据类型是否为整数类型
62    pub fn is_integer(&self) -> bool {
63        matches!(
64            self,
65            DataType::Int8 | DataType::Int16 | DataType::Int32 | DataType::Int64 | DataType::Int128 |
66            DataType::UInt8 | DataType::UInt16 | DataType::UInt32 | DataType::UInt64 | DataType::UInt128
67        )
68    }
69
70    /// 检查数据类型是否为数值类型(整数或浮点)
71    pub fn is_numeric(&self) -> bool {
72        matches!(
73            self,
74            DataType::Int8 | DataType::Int16 | DataType::Int32 | DataType::Int64 |
75            DataType::UInt8 | DataType::UInt16 | DataType::UInt32 | DataType::UInt64 |
76            DataType::Float32 | DataType::Float64
77        )
78    }
79}
80
81impl Ord for DataType {
82    fn cmp(&self, other: &Self) -> Ordering {
83        fn order_index(dt: &DataType) -> i32 {
84            match dt {
85                DataType::Null => 0,
86                DataType::Bool => 1,
87                DataType::Int8 => 10,
88                DataType::Int16 => 11,
89                DataType::Int32 => 12,
90                DataType::Int64 => 13,
91                DataType::Int128 => 14,
92                DataType::UInt8 => 20,
93                DataType::UInt16 => 21,
94                DataType::UInt32 => 22,
95                DataType::UInt64 => 23,
96                DataType::UInt128 => 24,
97                DataType::Float32 => 30,
98                DataType::Float64 => 31,
99                DataType::String => 40,
100                DataType::List(_) => 100,
101            }
102        }
103
104        match order_index(self).cmp(&order_index(other)) {
105            Ordering::Equal => {
106                match (self, other) {
107                    (DataType::List(a), DataType::List(b)) => a.cmp(b),
108                    _ => Ordering::Equal,
109                }
110            }
111            order => order,
112        }
113    }
114}
115
116impl PartialOrd for DataType {
117    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
118        Some(self.cmp(other))
119    }
120}
121
122/// 数据类型特征
123/// 
124/// 为 Rust 原生类型与 Axion DataType 枚举之间提供映射关系。
125/// 所有可以存储在 Series 中的类型都必须实现此特征。
126/// 
127/// # 要求
128/// 
129/// 实现此特征的类型必须满足:
130/// - `Debug` + `Clone` - 用于调试和克隆
131/// - `Send` + `Sync` - 用于多线程安全
132/// - `'static` - 确保类型生命周期足够长
133pub trait DataTypeTrait: Debug + Clone + Send + Sync + 'static {
134    /// 与此类型关联的 DataType 枚举变体
135    const DTYPE: DataType;
136
137    /// 获取实例的 DataType
138    /// 
139    /// 通常直接返回 `DTYPE` 常量
140    fn as_dtype(&self) -> DataType;
141}
142
143/// 为基础类型实现 DataTypeTrait 的宏
144macro_rules! impl_datatype_trait {
145    ($prim_type: ty, $dtype_variant: ident) => {
146        impl DataTypeTrait for $prim_type where $prim_type: Debug + Clone {
147            const DTYPE: DataType = DataType::$dtype_variant;
148
149            fn as_dtype(&self) -> DataType {
150                Self::DTYPE
151            }
152        }
153    };
154}
155
156// 为整数类型实现 DataTypeTrait
157impl_datatype_trait!(i8, Int8);
158impl_datatype_trait!(i16, Int16);
159impl_datatype_trait!(i32, Int32);
160impl_datatype_trait!(i64, Int64);
161impl_datatype_trait!(u8, UInt8);
162impl_datatype_trait!(u16, UInt16);
163impl_datatype_trait!(u32, UInt32);
164impl_datatype_trait!(u64, UInt64);
165
166// 为浮点类型实现 DataTypeTrait
167impl_datatype_trait!(f32, Float32);
168impl_datatype_trait!(f64, Float64);
169
170// 为布尔类型实现 DataTypeTrait
171impl_datatype_trait!(bool, Bool);
172
173// 为字符串类型手动实现 DataTypeTrait
174impl DataTypeTrait for String {
175    const DTYPE: DataType = DataType::String;
176
177    fn as_dtype(&self) -> DataType {
178        Self::DTYPE
179    }
180}