Skip to content

Commit

Permalink
update data::table::value traits.
Browse files Browse the repository at this point in the history
- change `data_value_is_copy` to return different values per variant.
- new method `data_values_are_copy` to replace the old `data_value_is_copy`.
- `Copy` bound from `DataType`, making it dyn-compatible and add it to `DataTypeCopy`.
- misc refactors and fixes.
- improve docs.
  • Loading branch information
joseluis committed Jan 27, 2025
1 parent 91c3fa3 commit 89f93fd
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 42 deletions.
8 changes: 7 additions & 1 deletion src/data/absence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ pub type NoData = ();
#[rustfmt::skip]
impl DataType for NoData {
type Value = NoData;
fn data_value_default(&self) -> Option<Self::Value> { Some(()) }

fn data_values_are_copy() -> bool { true }

fn data_value_is_copy(&self) -> bool { true }
fn data_value_default(&self) -> Option<Self::Value> { Some(()) }
fn data_value_align(&self) -> usize { align_of::<NoData>() }
fn data_value_size(&self) -> usize { size_of::<NoData>() }
}
#[rustfmt::skip]
impl DataValue for NoData {
type Type = NoData;

fn data_values_are_copy() -> bool { true }

fn data_type(&self) -> Self::Type {}
fn data_value_is_copy(&self) -> bool { true }
}
Expand Down
18 changes: 10 additions & 8 deletions src/data/table/value/macros/define_each.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ macro_rules! define_data_value {
)*
}

// alias DataValue Copy
// DataValue Copy (-With) alias
#[doc = $b "-bit data *value*, restricted to `Copy` variants." ]
///
/// See also:
Expand All @@ -121,13 +121,13 @@ macro_rules! define_data_value {
#[doc = "- [" [<$Value $b Copy With>] "][" [<$Value $b Copy With>] "] +With" ]
#[doc = "- [" [<$Value $b With>] "][" [<$Value $b With>] "] -Copy +With" ]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = $feature)))]
pub type [<$Value $b Copy>] = [< $Value $b With>]<$crate::NoData>;
pub type [<$Value $b Copy>] = [< $Value $b Copy With>]<$crate::NoData>;

// implement the DataValue trait
$crate::impl_data_value![
v: [< $Value $b Copy With >], DataValue,
t: [< $Type $b Copy With >], DataType,
is_copy: true,
all_are_copy: true,

copy:
$( $C_name, $C_type
Expand Down Expand Up @@ -242,7 +242,7 @@ macro_rules! define_data_value {
$crate::impl_data_value![
v: [< $Value $b With >], DataValue,
t: [< $Type $b With >], DataType,
is_copy: false,
all_are_copy: false,

copy:
$( $C_name, $C_type
Expand Down Expand Up @@ -475,10 +475,12 @@ macro_rules! define_data_type {
)*

$( // pointer-size & feature-gated dependencies
#[cfg(all($C_ptr_ptrdep, feature = $C_dep1_ptrdep,
#[cfg(all($C_ptr_ptrdep,
feature = $C_dep1_ptrdep,
feature = $C_dep2_ptrdep))]
#[cfg_attr(feature = "nightly_doc",
doc(cfg(all(feature = $C_dep1_ptrdep, feature = $C_dep2_ptrdep))))]
doc(cfg(all(feature = $C_dep1_ptrdep,
feature = $C_dep2_ptrdep))))]
#[doc = $C_doc_ptrdep]
$C_name_ptrdep,
)*
Expand All @@ -499,7 +501,7 @@ macro_rules! define_data_type {
$crate::impl_data_type![
v: [< $Value $b Copy With >], DataValue,
t: [< $Type $b Copy With >], DataType,
is_copy: true,
all_are_copy: true,

copy:
$( $C_name, $C_type,
Expand Down Expand Up @@ -620,7 +622,7 @@ macro_rules! define_data_type {
$crate::impl_data_type![
v: [< $Value $b With >], DataValue,
t: [< $Type $b With >], DataType,
is_copy: false,
all_are_copy: false,

copy:
$( $C_name, $C_type,
Expand Down
90 changes: 83 additions & 7 deletions src/data/table/value/macros/impl_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#[allow(unused_macros)]
macro_rules! impl_data_value {
(
v: $Vname:ident, $cbound:ident,
v: $Vname:ident, $vbound:ident,
t: $Tname:ident, $tbound:ident,
is_copy: $is_copy:stmt,
all_are_copy: $all_are_copy:stmt,

copy:
$( $C_name:ident, $C_type:ty
Expand Down Expand Up @@ -49,10 +49,48 @@ macro_rules! impl_data_value {
$N_dep1_ptrdep:literal, $N_dep2_ptrdep:literal
),* ;
) => { $crate::paste! {
impl<V: $cbound> $crate::DataValue for $Vname<V> {
impl<V: $vbound> $crate::DataValue for $Vname<V> {
type Type = $Tname<V::Type>;

fn data_value_is_copy(&self) -> bool { $is_copy }
fn data_values_are_copy() -> bool { $all_are_copy }

fn data_value_is_copy(&self) -> bool {
match self {
$Vname::None => true,
$Vname::Extra(t) => t.data_value_is_copy(),

$( $Vname::$C_name(_) => true, )*
$( $Vname::$N_name(_) => false, )*

$( // pointer-size dependant
#[cfg($C_ptr_ptr)]
$Vname::$C_name_ptr(_) => true,
)* $(
#[cfg($N_ptr_ptr)]
$Vname::$N_name_ptr(_) => false,
)*

$( // feature-gated dependencies
#[cfg(all(feature = $C_dep1_dep, feature = $C_dep2_dep))]
$Vname::$C_name_dep(_) => true,
)* $(
#[cfg(all(feature = $N_dep1_dep, feature = $N_dep2_dep))]
$Vname::$N_name_dep(_) => false,
)*

$( // pointer-size & feature-gated dependencies
#[cfg(all($C_ptr_ptrdep,
feature = $C_dep1_ptrdep,
feature = $C_dep2_ptrdep))]
$Vname::$C_name_ptrdep(_) => true,
)* $(
#[cfg(all($N_ptr_ptrdep,
feature = $N_dep1_ptrdep,
feature = $N_dep2_ptrdep))]
$Vname::$N_name_ptrdep(_) => false,
)*
}
}
fn data_type(&self) -> Self::Type {
match self {
$Vname::None => Self::Type::None,
Expand Down Expand Up @@ -100,9 +138,9 @@ pub(crate) use impl_data_value;
#[allow(unused_macros)]
macro_rules! impl_data_type {
(
v: $Vname:ident, $cbound:ident,
v: $Vname:ident, $vbound:ident,
t: $Tname:ident, $tbound:ident,
is_copy: $is_copy:stmt,
all_are_copy: $all_are_copy:stmt,

copy:
$( $C_name:ident, $C_type:ty,
Expand Down Expand Up @@ -149,7 +187,45 @@ macro_rules! impl_data_type {
impl<T: $tbound> $crate::DataType for $Tname<T> {
type Value = $Vname<T::Value>;

fn data_value_is_copy(&self) -> bool { $is_copy }
fn data_values_are_copy() -> bool { $all_are_copy }

fn data_value_is_copy(&self) -> bool {
match self {
$Tname::None => true,
$Tname::Extra(t) => t.data_value_is_copy(),

$( $Tname::$C_name => true, )*
$( $Tname::$N_name => false, )*

$( // pointer-size dependant
#[cfg($C_ptr_ptr)]
$Tname::$C_name_ptr => true,
)* $(
#[cfg($N_ptr_ptr)]
$Tname::$N_name_ptr => false,
)*

$( // feature-gated dependencies
#[cfg(all(feature = $C_dep1_dep, feature = $C_dep2_dep))]
$Tname::$C_name_dep => true,
)* $(
#[cfg(all(feature = $N_dep1_dep, feature = $N_dep2_dep))]
$Tname::$N_name_dep => false,
)*

$( // pointer-size & feature-gated dependencies
#[cfg(all($C_ptr_ptrdep,
feature = $C_dep1_ptrdep,
feature = $C_dep2_ptrdep))]
$Tname::$C_name_ptrdep => true,
)* $(
#[cfg(all($N_ptr_ptrdep,
feature = $N_dep1_ptrdep,
feature = $N_dep2_ptrdep))]
$Tname::$N_name_ptrdep => false,
)*
}
}

fn data_value_default(&self) -> Option<Self::Value> {
match self {
Expand Down
54 changes: 28 additions & 26 deletions src/data/table/value/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,35 @@

use core::fmt::Debug;

/// Common trait for *data types*.
/// Common trait for enumerating *data types*.
///
/// Allows extending `DataType*`**`With`** versions with custom *types*.
///
/// # See also
/// - [`DataTypeCopy`]
/// - [`DataValueCopy`]
/// - [`DataValue`]
pub trait DataType: Copy + Debug {
pub trait DataType: Debug {
/// The `DataValue` type that pairs with this `DataType`.
type Value: DataValue;

/// Returns some default value corresponding to the current type.
///
/// Or returns `None` if the actual type doesn't implement `Default`.
fn data_value_default(&self) -> Option<Self::Value>;
/// Returns whether all values represented by this type are `Copy`.
fn data_values_are_copy() -> bool;

/// Returns true if the data represented by this type is [`Copy`].
/// Returns whether the specific value for this type is `Copy`.
fn data_value_is_copy(&self) -> bool;

/// Returns the alignment of the data represented by the current type.
/// Returns the default value for this type, or `None` if not available.
fn data_value_default(&self) -> Option<Self::Value>;

/// Returns the alignment of the value represented by this type.
fn data_value_align(&self) -> usize;

/// Returns the size of the data represented by this type.
/// Returns the size of the value represented by this type.
fn data_value_size(&self) -> usize;
}

/// Common marker trait for `Copy` *data types*.
/// Common trait for enumerating `Copy`-constrained *data types*.
///
/// Allows extending `DataType*Copy`**`With`** versions with custom *types*.
///
Expand All @@ -51,21 +52,19 @@ pub trait DataType: Copy + Debug {
/// - [`DataType`]
/// - [`DataValue`]
/// - [`DataValueCopy`]
pub trait DataTypeCopy: DataType
pub trait DataTypeCopy: DataType + Copy
where
Self::Value: DataValueCopy,
{
/// Returns some default value corresponding to the current (Copy) type.
///
/// Or returns `None` if the actual type doesn't implement `Default`.
/// Returns the default value for this `Copy` type, or `None` if not available.
///
/// The default implementation calls [`DataType::data_value_default`].
/// The default implementation forwards to [`DataType::data_value_default`].
fn data_value_copy_default(&self) -> Option<Self::Value> {
self.data_value_default()
}
}

/// Common trait for *data values*.
/// Common trait for enumerating *data values*.
///
/// Allows extending `DataValue*`**`With`** versions.
///
Expand All @@ -77,15 +76,17 @@ pub trait DataValue: Debug {
/// The `DataType` type that pairs with this `DataValue`.
type Type: DataType;

/// Returns the data type corresponding to the current value.
/// Returns whether all values are `Copy`.
fn data_values_are_copy() -> bool;

/// Returns the data type of this value.
fn data_type(&self) -> Self::Type;

/// Whether the data type in the current variant is [`Copy`].
// MAYBE DELETE
/// Returns whether the specific value is `Copy`.
fn data_value_is_copy(&self) -> bool;
}

/// Common marker trait for `Copy` *data values*.
/// Common trait for enumerating `Copy`-constrained *data values*.
///
/// Allows extending `DataValue*Copy`**`With`** versions.
///
Expand All @@ -101,26 +102,27 @@ pub trait DataValue: Debug {
//
// NOTE we must not require `where Self::Type: DataTypeCopy` to avoid loops.
pub trait DataValueCopy: DataValue + Copy {
/// Returns the data type corresponding to the current (Copy) value.
/// Returns the data type associated with this `Copy` value.
///
/// The default implementation calls [`DataValue::data_type`].
/// The default implementation forwards to [`DataValue::data_type`].
fn data_type_copy(&self) -> Self::Type {
self.data_type()
}
}

/// Common trait for *unsafe data values*.
/// Common unsafe trait for enumerating *raw data values* without type metadata.
///
/// # Safety
/// You have to now what you're doing.
/// You have to know what you're doing.
#[cfg(feature = "unsafe_layout")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "unsafe_layout")))]
pub unsafe trait DataRaw {}

/// Comon marker trait for *unsafe* `Copy` *data values*.
/// Common unsafe trait for enumerating `Copy`-constrained *raw data values*
/// without type metadata.
///
/// # Safety
/// You have to now what you're doing.
/// You have to know what you're doing.
#[cfg(feature = "unsafe_layout")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "unsafe_layout")))]
pub unsafe trait DataRawCopy: DataRaw + Copy {}

0 comments on commit 89f93fd

Please sign in to comment.