devela/data/table/value/traits.rs
1// devela::data::table::value::traits
2//
3//! Defines the traits [`DataType`], [`DataValue`], [`DataRaw`], and related.
4//
5// - DataType
6// - DataTypeCopy
7// - DataValue
8// - DataValueCopy
9// - DataRaw
10// - DataRawCopy
11
12use core::fmt::Debug;
13
14/// Common trait for enumerating *data types*.
15///
16/// Allows extending `DataType*`**`With`** versions with custom *types*.
17///
18/// # See also
19/// - [`DataTypeCopy`]
20/// - [`DataValueCopy`]
21/// - [`DataValue`]
22pub trait DataType: Debug {
23 /// The `DataValue` type that pairs with this `DataType`.
24 type Value: DataValue;
25
26 /// Returns whether all values represented by this type are `Copy`.
27 fn data_values_are_copy() -> bool;
28
29 /// Returns whether the specific value for this type is `Copy`.
30 fn data_value_is_copy(&self) -> bool;
31
32 /// Returns the default value for this type, or `None` if not available.
33 fn data_value_default(&self) -> Option<Self::Value>;
34
35 /// Returns the alignment of the value represented by this type.
36 fn data_value_align(&self) -> usize;
37
38 /// Returns the size of the value represented by this type.
39 fn data_value_size(&self) -> usize;
40}
41
42/// Common trait for enumerating `Copy`-constrained *data types*.
43///
44/// Allows extending `DataType*Copy`**`With`** versions with custom *types*.
45///
46/// # Coherence
47///
48/// The `DataType::`[`is_copy`][DataType#method.is_copy]
49/// super-trait method should probably return `true` as well.
50///
51/// # See also
52/// - [`DataType`]
53/// - [`DataValue`]
54/// - [`DataValueCopy`]
55pub trait DataTypeCopy: DataType + Copy
56where
57 Self::Value: DataValueCopy,
58{
59 /// Returns the default value for this `Copy` type, or `None` if not available.
60 ///
61 /// The default implementation forwards to [`DataType::data_value_default`].
62 fn data_value_copy_default(&self) -> Option<Self::Value> {
63 self.data_value_default()
64 }
65}
66
67/// Common trait for enumerating *data values*.
68///
69/// Allows extending `DataValue*`**`With`** versions.
70///
71/// See also:
72/// - [`DataValueCopy`]
73/// - [`DataTypeCopy`]
74/// - [`DataType`]
75pub trait DataValue: Debug {
76 /// The `DataType` type that pairs with this `DataValue`.
77 type Type: DataType;
78
79 /// Returns whether all values are `Copy`.
80 fn data_values_are_copy() -> bool;
81
82 /// Returns the data type of this value.
83 fn data_type(&self) -> Self::Type;
84
85 /// Returns whether the specific value is `Copy`.
86 fn data_value_is_copy(&self) -> bool;
87}
88
89/// Common trait for enumerating `Copy`-constrained *data values*.
90///
91/// Allows extending `DataValue*Copy`**`With`** versions.
92///
93/// # Coherence
94///
95/// The `DataValue::`[`is_copy`][DataValue#method.is_copy]
96/// super-trait method should probably return `true` as well.
97///
98/// # See also
99/// - [`DataValue`]
100/// - [`DataType`]
101/// - [`DataTypeCopy`]
102//
103// NOTE we must not require `where Self::Type: DataTypeCopy` to avoid loops.
104pub trait DataValueCopy: DataValue + Copy {
105 /// Returns the data type associated with this `Copy` value.
106 ///
107 /// The default implementation forwards to [`DataValue::data_type`].
108 fn data_type_copy(&self) -> Self::Type {
109 self.data_type()
110 }
111}
112
113/// Common unsafe trait for enumerating *raw data values* without type metadata.
114///
115/// # Safety
116/// You have to know what you're doing.
117#[cfg(feature = "unsafe_layout")]
118#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "unsafe_layout")))]
119pub unsafe trait DataRaw {}
120
121/// Common unsafe trait for enumerating `Copy`-constrained *raw data values*
122/// without type metadata.
123///
124/// # Safety
125/// You have to know what you're doing.
126#[cfg(feature = "unsafe_layout")]
127#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "unsafe_layout")))]
128pub unsafe trait DataRawCopy: DataRaw + Copy {}