tea_dyn/lib.rs
1mod backends_impl;
2
3use tea_core::prelude::{Vec1, Vec1View};
4use tea_dtype::{DataType, GetDataType, IsNone};
5use tea_error::TResult;
6
7/// Trait for types that can be converted into a dynamic vector representation.
8pub trait IntoDyn {
9 /// The dynamic vector type that this type can be converted into.
10 type Dyn: DynVec1;
11
12 /// Converts this type into its dynamic vector representation.
13 fn into_dyn(self) -> Self::Dyn;
14}
15
16/// Trait for dynamic vector types that can hold various data types.
17pub trait DynVec1 {
18 /// The type used for boolean items in the vector.
19 type BoolItem: IsNone<Inner = bool>;
20 /// The type used for 64-bit floating point items in the vector.
21 type F64Item: IsNone<Inner = f64>;
22 /// The type used for 32-bit floating point items in the vector.
23 type F32Item: IsNone<Inner = f32>;
24 /// The type used for 64-bit integer items in the vector.
25 type I64Item: IsNone<Inner = i64>;
26 /// The type used for 32-bit integer items in the vector.
27 type I32Item: IsNone<Inner = i32>;
28 type StrItem<'a>: IsNone<Inner = &'a str>;
29
30 /// The vector type for boolean items.
31 type BoolVec: Vec1<Self::BoolItem> + IntoDyn<Dyn = Self>;
32 /// The vector type for 64-bit floating point items.
33 type F64Vec: Vec1<Self::F64Item> + IntoDyn<Dyn = Self>;
34 /// The vector type for 32-bit floating point items.
35 type F32Vec: Vec1<Self::F32Item> + IntoDyn<Dyn = Self>;
36 /// The vector type for 64-bit integer items.
37 type I64Vec: Vec1<Self::I64Item> + IntoDyn<Dyn = Self>;
38 /// The vector type for 32-bit integer items.
39 type I32Vec: Vec1<Self::I32Item> + IntoDyn<Dyn = Self>;
40 // type StrVec<'a>: Vec1<Self::StrItem<'a>> + IntoDyn<Dyn = Self>;
41 /// Returns the data type of the vector.
42 fn get_dtype(&self) -> DataType;
43 /// Casts the vector to a new data type.
44 fn cast_into(self, dtype: DataType) -> TResult<Self>
45 where
46 Self: Sized;
47
48 /// Extracts a view of the vector as boolean items.
49 fn extract_bool(&self) -> TResult<impl Vec1View<Self::BoolItem>>;
50
51 /// Extracts a view of the vector as 64-bit floating point items.
52 fn extract_f64(&self) -> TResult<impl Vec1View<Self::F64Item>>;
53
54 /// Extracts a view of the vector as 32-bit floating point items.
55 fn extract_f32(&self) -> TResult<impl Vec1View<Self::F32Item>>;
56
57 /// Extracts a view of the vector as 64-bit integer items.
58 fn extract_i64(&self) -> TResult<impl Vec1View<Self::I64Item>>;
59
60 /// Extracts a view of the vector as 32-bit integer items.
61 fn extract_i32(&self) -> TResult<impl Vec1View<Self::I32Item>>;
62
63 /// Returns the name of the vector, if available.
64 #[inline]
65 fn get_name(&self) -> Option<&str> {
66 None
67 }
68
69 /// Renames the vector and returns a mutable reference to self.
70 #[inline]
71 fn rename(&mut self, _name: impl AsRef<str>) -> &mut Self {
72 self
73 }
74
75 /// Returns a new vector with the given name.
76 #[inline]
77 fn with_name(mut self, name: impl AsRef<str>) -> Self
78 where
79 Self: Sized,
80 {
81 self.rename(name);
82 self
83 }
84
85 /// Casts the vector to a new type T.
86 #[inline]
87 fn cast<T: GetDataType>(self) -> TResult<Self>
88 where
89 Self: Sized,
90 {
91 let dtype = self.get_dtype();
92 self.cast_into(dtype)
93 }
94}