Skip to content

Remove macros from some trait implementation #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 36 additions & 46 deletions src/core/arith.rs
Original file line number Diff line number Diff line change
@@ -370,39 +370,24 @@ pub trait Convertable {
/// This type alias always points to `Self` which is the
/// type of [Array](./struct.Array.html) returned by the
/// trait method [convert](./trait.Convertable.html#tymethod.convert).
type OutType;
type OutType: HasAfEnum;

/// Get an Array of implementors type
fn convert(&self) -> Array<Self::OutType>
where
<Self as Convertable>::OutType: HasAfEnum;
fn convert(&self) -> Array<Self::OutType>;
}

macro_rules! convertable_type_def {
($rust_type: ty) => {
impl Convertable for $rust_type {
type OutType = $rust_type;
impl<T> Convertable for T
where
T: Clone + HasAfEnum + ConstGenerator<OutType = T>,
<T as ConstGenerator>::OutType: HasAfEnum,
{
type OutType = T;

fn convert(&self) -> Array<Self::OutType> {
constant(*self, Dim4::new(&[1, 1, 1, 1]))
}
}
};
fn convert(&self) -> Array<Self::OutType> {
constant(self.clone(), Dim4::new(&[1, 1, 1, 1]))
}
}

convertable_type_def!(Complex<f64>);
convertable_type_def!(Complex<f32>);
convertable_type_def!(u64);
convertable_type_def!(i64);
convertable_type_def!(f64);
convertable_type_def!(f32);
convertable_type_def!(i32);
convertable_type_def!(u32);
convertable_type_def!(i16);
convertable_type_def!(u16);
convertable_type_def!(u8);
convertable_type_def!(bool);

impl<T: HasAfEnum> Convertable for Array<T> {
type OutType = T;

@@ -707,44 +692,45 @@ where
}
}

macro_rules! arith_scalar_func {
($rust_type: ty, $op_name:ident, $fn_name: ident) => {
macro_rules! arith_rhs_scalar_func {
($op_name:ident, $fn_name: ident) => {
// Implement (&Array<T> op_name rust_type)
impl<'f, T> $op_name<$rust_type> for &'f Array<T>
impl<'f, T, U> $op_name<U> for &'f Array<T>
where
T: HasAfEnum + ImplicitPromote<$rust_type>,
$rust_type: HasAfEnum + ImplicitPromote<T>,
<T as ImplicitPromote<$rust_type>>::Output: HasAfEnum,
T: HasAfEnum + ImplicitPromote<U>,
U: HasAfEnum + ImplicitPromote<T> + Clone + ConstGenerator<OutType = U>,
{
type Output = Array<<T as ImplicitPromote<$rust_type>>::Output>;
type Output = Array<<T as ImplicitPromote<U>>::Output>;

fn $fn_name(self, rhs: $rust_type) -> Self::Output {
fn $fn_name(self, rhs: U) -> Self::Output {
let temp = rhs.clone();
$fn_name(self, &temp, false)
}
}

// Implement (Array<T> op_name rust_type)
impl<T: HasAfEnum> $op_name<$rust_type> for Array<T>
impl<T, U> $op_name<U> for Array<T>
where
T: HasAfEnum + ImplicitPromote<$rust_type>,
$rust_type: HasAfEnum + ImplicitPromote<T>,
<T as ImplicitPromote<$rust_type>>::Output: HasAfEnum,
T: HasAfEnum + ImplicitPromote<U>,
U: HasAfEnum + ImplicitPromote<T> + Clone + ConstGenerator<OutType = U>,
{
type Output = Array<<T as ImplicitPromote<$rust_type>>::Output>;
type Output = Array<<T as ImplicitPromote<U>>::Output>;

fn $fn_name(self, rhs: $rust_type) -> Self::Output {
fn $fn_name(self, rhs: U) -> Self::Output {
let temp = rhs.clone();
$fn_name(&self, &temp, false)
}
}
};
}

macro_rules! arith_lhs_scalar_func {
($rust_type: ty, $op_name: ident, $fn_name: ident) => {
// Implement (rust_type op_name &Array<T>)
impl<'f, T> $op_name<&'f Array<T>> for $rust_type
where
T: HasAfEnum + ImplicitPromote<$rust_type>,
$rust_type: HasAfEnum + ImplicitPromote<T>,
<$rust_type as ImplicitPromote<T>>::Output: HasAfEnum,
{
type Output = Array<<$rust_type as ImplicitPromote<T>>::Output>;

@@ -758,7 +744,6 @@ macro_rules! arith_scalar_func {
where
T: HasAfEnum + ImplicitPromote<$rust_type>,
$rust_type: HasAfEnum + ImplicitPromote<T>,
<$rust_type as ImplicitPromote<T>>::Output: HasAfEnum,
{
type Output = Array<<$rust_type as ImplicitPromote<T>>::Output>;

@@ -769,12 +754,17 @@ macro_rules! arith_scalar_func {
};
}

arith_rhs_scalar_func!(Add, add);
arith_rhs_scalar_func!(Sub, sub);
arith_rhs_scalar_func!(Mul, mul);
arith_rhs_scalar_func!(Div, div);

macro_rules! arith_scalar_spec {
($ty_name:ty) => {
arith_scalar_func!($ty_name, Add, add);
arith_scalar_func!($ty_name, Sub, sub);
arith_scalar_func!($ty_name, Mul, mul);
arith_scalar_func!($ty_name, Div, div);
arith_lhs_scalar_func!($ty_name, Add, add);
arith_lhs_scalar_func!($ty_name, Sub, sub);
arith_lhs_scalar_func!($ty_name, Mul, mul);
arith_lhs_scalar_func!($ty_name, Div, div);
};
}

6 changes: 2 additions & 4 deletions src/core/data.rs
Original file line number Diff line number Diff line change
@@ -126,16 +126,14 @@ extern "C" {
///
pub trait ConstGenerator {
/// The type of Array<T> object returned by generate function
type OutType;
type OutType: HasAfEnum;

/// Create an Array of `dims` size from scalar value `self`.
///
/// # Parameters
///
/// - `dims` are the dimensions of the output constant [Array](./struct.Array.html)
fn generate(&self, dims: Dim4) -> Array<Self::OutType>
where
Self::OutType: HasAfEnum;
fn generate(&self, dims: Dim4) -> Array<Self::OutType>;
}

impl ConstGenerator for i64 {
39 changes: 17 additions & 22 deletions src/core/util.rs
Original file line number Diff line number Diff line change
@@ -141,27 +141,27 @@ impl From<u32> for ColorMap {
///
pub trait HasAfEnum {
/// This type alias points to `Self` always.
type InType;
type InType: HasAfEnum;
/// This type alias points to the data type used to hold real part of a
/// complex number. For real valued numbers, this points to `Self`.
type BaseType;
type BaseType: HasAfEnum;
/// This type alias points to `f32` for all 32 bit size types and `f64` for
/// larger 64-bit size types.
type AbsOutType;
type AbsOutType: HasAfEnum;
/// This type alias points to `f64`/`f32` for floating point types and
/// `Self` otherwise.
type ArgOutType;
type ArgOutType: HasAfEnum;
/// This type alias is used to define the output Array type for unary
/// operations. It points to `Self` for floating point types, either
/// real or complex. It points to `f32` for rest of the input types.
type UnaryOutType;
type UnaryOutType: HasAfEnum;
/// This type alias points to complex type created from a given input type.
/// This alias always points to either `std::Complex<f32>` or `std::Complex<f64>`
type ComplexOutType;
/// This type alias points to a data type that can store the mean value for
/// a given input type. This alias points to `f32`/`Complex<f32>` for all 32
/// bit size types and `f64`/`Complex<f64>` for larger 64-bit size types.
type MeanOutType;
type MeanOutType: HasAfEnum;
/// This type alias points to a data type that can store the result of
/// aggregation of set of values for a given input type. Aggregate type
/// alias points to below types for given input types:
@@ -172,17 +172,17 @@ pub trait HasAfEnum {
/// - `u32` for input types: `u16`
/// - `i32` for input types: `i32`
/// - `u32` for input types: `u32`
type AggregateOutType;
type AggregateOutType: HasAfEnum;
/// This type is different for b8 input type
type ProductOutType;
type ProductOutType: HasAfEnum;
/// This type alias points to the output type for given input type of
/// sobel filter operation. Sobel filter output alias points to below
/// types for given input types:
/// - `f32` for input types: `Complex<f32>`, `f32`
/// - `f64` for input types: `Complex<f64>`, `f64`
/// - `i32` for input types: `bool`, `u8`, `i16`, `u16`, `i32`, `u32`
/// - `i64` for input types: `i64`, `u64`
type SobelOutType;
type SobelOutType: HasAfEnum;

/// Return trait implmentors corresponding [DType](./enum.DType.html)
fn get_af_dtype() -> DType;
@@ -427,7 +427,14 @@ impl From<u32> for RandomEngineType {
pub trait ImplicitPromote<RHS> {
/// This type alias points to the type of the result obtained
/// by performing a given binary option on given type and `RHS`.
type Output;
type Output: HasAfEnum;
}

impl<T> ImplicitPromote<T> for T
where
T: HasAfEnum,
{
type Output = T;
}

macro_rules! implicit {
@@ -443,7 +450,6 @@ macro_rules! implicit {
//

//LHS is Complex double
implicit!(c64, c64 => c64);
implicit!(c64, c32 => c64);
implicit!(c64, f64 => c64);
implicit!(c64, f32 => c64);
@@ -458,7 +464,6 @@ implicit!(c64, u8 => c64);

//LHS is Complex float
implicit!(c32, c64 => c64);
implicit!(c32, c32 => c32);
implicit!(c32, f64 => c64);
implicit!(c32, f32 => c32);
implicit!(c32, i64 => c32);
@@ -473,7 +478,6 @@ implicit!(c32, u8 => c32);
//LHS is 64-bit floating point
implicit!(f64, c64 => c64);
implicit!(f64, c32 => c64);
implicit!(f64, f64 => f64);
implicit!(f64, f32 => f64);
implicit!(f64, i64 => f64);
implicit!(f64, u64 => f64);
@@ -488,7 +492,6 @@ implicit!(f64, u8 => f64);
implicit!(f32, c64 => c64);
implicit!(f32, c32 => c32);
implicit!(f32, f64 => f64);
implicit!(f32, f32 => f32);
implicit!(f32, i64 => f32);
implicit!(f32, u64 => f32);
implicit!(f32, i32 => f32);
@@ -503,7 +506,6 @@ implicit!(i64, c64 => c64);
implicit!(i64, c32 => c32);
implicit!(i64, f64 => f64);
implicit!(i64, f32 => f32);
implicit!(i64, i64 => i64);
implicit!(i64, u64 => u64);
implicit!(i64, i32 => i64);
implicit!(i64, u32 => i64);
@@ -518,7 +520,6 @@ implicit!(u64, c32 => c32);
implicit!(u64, f64 => f64);
implicit!(u64, f32 => f32);
implicit!(u64, i64 => u64);
implicit!(u64, u64 => u64);
implicit!(u64, i32 => u64);
implicit!(u64, u32 => u64);
implicit!(u64, i16 => u64);
@@ -533,7 +534,6 @@ implicit!(i32, f64 => f64);
implicit!(i32, f32 => f32);
implicit!(i32, i64 => i64);
implicit!(i32, u64 => u64);
implicit!(i32, i32 => i32);
implicit!(i32, u32 => u32);
implicit!(i32, i16 => i32);
implicit!(i32, u16 => i32);
@@ -548,7 +548,6 @@ implicit!(u32, f32 => f32);
implicit!(u32, i64 => i64);
implicit!(u32, u64 => u64);
implicit!(u32, i32 => u32);
implicit!(u32, u32 => u32);
implicit!(u32, i16 => u32);
implicit!(u32, u16 => u32);
implicit!(u32, bool => u32);
@@ -563,7 +562,6 @@ implicit!(i16, i64 => i64);
implicit!(i16, u64 => u64);
implicit!(i16, i32 => i32);
implicit!(i16, u32 => u32);
implicit!(i16, i16 => i16);
implicit!(i16, u16 => u16);
implicit!(i16, bool => u16);
implicit!(i16, u8 => u16);
@@ -578,7 +576,6 @@ implicit!(u16, u64 => u64);
implicit!(u16, i32 => i32);
implicit!(u16, u32 => u32);
implicit!(u16, i16 => u16);
implicit!(u16, u16 => u16);
implicit!(u16, bool => u16);
implicit!(u16, u8 => u16);

@@ -594,7 +591,6 @@ implicit!(u8, u32 => u32);
implicit!(u8, i16 => i16);
implicit!(u8, u16 => u16);
implicit!(u8, bool => u8);
implicit!(u8, u8 => u8);

//LHS is bool(af::s8)
implicit!(bool, c64 => c64);
@@ -607,7 +603,6 @@ implicit!(bool, i32 => i32);
implicit!(bool, u32 => u32);
implicit!(bool, i16 => i16);
implicit!(bool, u16 => u16);
implicit!(bool, bool => bool);
implicit!(bool, u8 => u8);

///Trait qualifier to accept either real or complex typed data