From c3bfdb70541b48c1b6ed2b3ad6d770b0de38800f Mon Sep 17 00:00:00 2001 From: pradeep Date: Mon, 7 Dec 2020 16:41:52 +0530 Subject: [PATCH] Shorten trait bounds in arith, data modules --- src/core/arith.rs | 109 ++++++++++++++++++---------------------------- src/core/data.rs | 6 +-- src/core/util.rs | 28 ++++++------ 3 files changed, 60 insertions(+), 83 deletions(-) diff --git a/src/core/arith.rs b/src/core/arith.rs index fb7fab4cc..901074597 100644 --- a/src/core/arith.rs +++ b/src/core/arith.rs @@ -287,9 +287,8 @@ macro_rules! binary_func { /// - Only one element in `lhs` or `rhs` along a given dimension/axis pub fn $fn_name(lhs: &Array, rhs: &Array, batch: bool) -> Array where - A: HasAfEnum + ImplicitPromote, - B: HasAfEnum + ImplicitPromote, - >::Output: HasAfEnum, + A: ImplicitPromote, + B: ImplicitPromote, { unsafe { let mut temp: af_array = std::ptr::null_mut(); @@ -378,8 +377,7 @@ pub trait Convertable { impl Convertable for T where - T: Clone + HasAfEnum + ConstGenerator, - ::OutType: HasAfEnum, + T: Clone + ConstGenerator, { type OutType = T; @@ -400,9 +398,8 @@ macro_rules! overloaded_binary_func { ($doc_str: expr, $fn_name: ident, $help_name: ident, $ffi_name: ident) => { fn $help_name(lhs: &Array, rhs: &Array, batch: bool) -> Array where - A: HasAfEnum + ImplicitPromote, - B: HasAfEnum + ImplicitPromote, - >::Output: HasAfEnum, + A: ImplicitPromote, + B: ImplicitPromote, { unsafe { let mut temp: af_array = std::ptr::null_mut(); @@ -453,10 +450,8 @@ macro_rules! overloaded_binary_func { where T: Convertable, U: Convertable, - ::OutType: HasAfEnum + ImplicitPromote<::OutType>, - ::OutType: HasAfEnum + ImplicitPromote<::OutType>, - <::OutType as ImplicitPromote<::OutType>>::Output: - HasAfEnum, + ::OutType: ImplicitPromote<::OutType>, + ::OutType: ImplicitPromote<::OutType>, { let lhs = arg1.convert(); // Convert to Array let rhs = arg2.convert(); // Convert to Array @@ -507,8 +502,8 @@ macro_rules! overloaded_compare_func { ($doc_str: expr, $fn_name: ident, $help_name: ident, $ffi_name: ident) => { fn $help_name(lhs: &Array, rhs: &Array, batch: bool) -> Array where - A: HasAfEnum + ImplicitPromote, - B: HasAfEnum + ImplicitPromote, + A: ImplicitPromote, + B: ImplicitPromote, { unsafe { let mut temp: af_array = std::ptr::null_mut(); @@ -557,8 +552,8 @@ macro_rules! overloaded_compare_func { where T: Convertable, U: Convertable, - ::OutType: HasAfEnum + ImplicitPromote<::OutType>, - ::OutType: HasAfEnum + ImplicitPromote<::OutType>, + ::OutType: ImplicitPromote<::OutType>, + ::OutType: ImplicitPromote<::OutType>, { let lhs = arg1.convert(); // Convert to Array let rhs = arg2.convert(); // Convert to Array @@ -615,9 +610,8 @@ fn clamp_helper( batch: bool, ) -> Array<>::Output> where - X: HasAfEnum + ImplicitPromote, - Y: HasAfEnum + ImplicitPromote, - >::Output: HasAfEnum, + X: ImplicitPromote, + Y: ImplicitPromote, { unsafe { let mut temp: af_array = std::ptr::null_mut(); @@ -667,10 +661,9 @@ pub fn clamp( batch: bool, ) -> Array<::OutType>>::Output> where - T: HasAfEnum + ImplicitPromote<::OutType>, + T: ImplicitPromote<::OutType>, C: Convertable, - ::OutType: HasAfEnum + ImplicitPromote, - ::OutType>>::Output: HasAfEnum, + ::OutType: ImplicitPromote, { let lo = arg1.convert(); // Convert to Array let hi = arg2.convert(); // Convert to Array @@ -697,8 +690,8 @@ macro_rules! arith_rhs_scalar_func { // Implement (&Array op_name rust_type) impl<'f, T, U> $op_name for &'f Array where - T: HasAfEnum + ImplicitPromote, - U: HasAfEnum + ImplicitPromote + Clone + ConstGenerator, + T: ImplicitPromote, + U: ImplicitPromote + Clone + ConstGenerator, { type Output = Array<>::Output>; @@ -711,8 +704,8 @@ macro_rules! arith_rhs_scalar_func { // Implement (Array op_name rust_type) impl $op_name for Array where - T: HasAfEnum + ImplicitPromote, - U: HasAfEnum + ImplicitPromote + Clone + ConstGenerator, + T: ImplicitPromote, + U: ImplicitPromote + Clone + ConstGenerator, { type Output = Array<>::Output>; @@ -729,8 +722,8 @@ macro_rules! arith_lhs_scalar_func { // Implement (rust_type op_name &Array) impl<'f, T> $op_name<&'f Array> for $rust_type where - T: HasAfEnum + ImplicitPromote<$rust_type>, - $rust_type: HasAfEnum + ImplicitPromote, + T: ImplicitPromote<$rust_type>, + $rust_type: ImplicitPromote, { type Output = Array<<$rust_type as ImplicitPromote>::Output>; @@ -742,8 +735,8 @@ macro_rules! arith_lhs_scalar_func { // Implement (rust_type op_name Array) impl $op_name> for $rust_type where - T: HasAfEnum + ImplicitPromote<$rust_type>, - $rust_type: HasAfEnum + ImplicitPromote, + T: ImplicitPromote<$rust_type>, + $rust_type: ImplicitPromote, { type Output = Array<<$rust_type as ImplicitPromote>::Output>; @@ -782,9 +775,8 @@ macro_rules! arith_func { ($op_name:ident, $fn_name:ident, $delegate:ident) => { impl $op_name> for Array where - A: HasAfEnum + ImplicitPromote, - B: HasAfEnum + ImplicitPromote, - >::Output: HasAfEnum, + A: ImplicitPromote, + B: ImplicitPromote, { type Output = Array<>::Output>; @@ -795,9 +787,8 @@ macro_rules! arith_func { impl<'a, A, B> $op_name<&'a Array> for Array where - A: HasAfEnum + ImplicitPromote, - B: HasAfEnum + ImplicitPromote, - >::Output: HasAfEnum, + A: ImplicitPromote, + B: ImplicitPromote, { type Output = Array<>::Output>; @@ -808,9 +799,8 @@ macro_rules! arith_func { impl<'a, A, B> $op_name> for &'a Array where - A: HasAfEnum + ImplicitPromote, - B: HasAfEnum + ImplicitPromote, - >::Output: HasAfEnum, + A: ImplicitPromote, + B: ImplicitPromote, { type Output = Array<>::Output>; @@ -821,9 +811,8 @@ macro_rules! arith_func { impl<'a, 'b, A, B> $op_name<&'a Array> for &'b Array where - A: HasAfEnum + ImplicitPromote, - B: HasAfEnum + ImplicitPromote, - >::Output: HasAfEnum, + A: ImplicitPromote, + B: ImplicitPromote, { type Output = Array<>::Output>; @@ -849,9 +838,8 @@ macro_rules! bitshift_scalar_func { ($rust_type: ty, $trait_name: ident, $op_name: ident) => { impl $trait_name<$rust_type> for Array where - T: HasAfEnum + ImplicitPromote<$rust_type>, - $rust_type: HasAfEnum + ImplicitPromote, - >::Output: HasAfEnum, + T: ImplicitPromote<$rust_type>, + $rust_type: ImplicitPromote, { type Output = Array<>::Output>; @@ -862,9 +850,8 @@ macro_rules! bitshift_scalar_func { } impl<'f, T> $trait_name<$rust_type> for &'f Array where - T: HasAfEnum + ImplicitPromote<$rust_type>, - $rust_type: HasAfEnum + ImplicitPromote, - >::Output: HasAfEnum, + T: ImplicitPromote<$rust_type>, + $rust_type: ImplicitPromote, { type Output = Array<>::Output>; @@ -900,10 +887,8 @@ mod op_assign { ($op_name:ident, $fn_name:ident, $func: ident) => { impl $op_name> for Array where - A: HasAfEnum + ImplicitPromote, - B: HasAfEnum + ImplicitPromote, - >::Output: HasAfEnum, - >::Output: HasAfEnum, + A: ImplicitPromote, + B: ImplicitPromote, { fn $fn_name(&mut self, rhs: Array) { let tmp_seq = Seq::::default(); @@ -930,10 +915,8 @@ mod op_assign { ($rust_type:ty, $trait_name:ident, $op_name:ident, $func:ident) => { impl $trait_name<$rust_type> for Array where - $rust_type: HasAfEnum + ImplicitPromote, - T: HasAfEnum - + ImplicitPromote<$rust_type> - + ImplicitPromote<$rust_type, Output = T>, + $rust_type: ImplicitPromote, + T: ImplicitPromote<$rust_type, Output = T>, { fn $op_name(&mut self, rhs: $rust_type) { let mut temp = $func(self, &rhs, false); @@ -959,10 +942,8 @@ mod op_assign { ($op_name:ident, $fn_name:ident, $func: ident) => { impl $op_name> for Array where - A: HasAfEnum + ImplicitPromote, - B: HasAfEnum + ImplicitPromote, - >::Output: HasAfEnum, - >::Output: HasAfEnum, + A: ImplicitPromote, + B: ImplicitPromote, { fn $fn_name(&mut self, rhs: Array) { let tmp_seq = Seq::::default(); @@ -985,13 +966,9 @@ mod op_assign { ///Implement negation trait for Array impl Neg for Array where - T: HasAfEnum + Zero + ConstGenerator, - ::OutType: HasAfEnum, - ::OutType: ImplicitPromote, - T: ImplicitPromote<::OutType>, - <::OutType as ImplicitPromote>::Output: HasAfEnum, + T: Zero + ConstGenerator, { - type Output = Array<<::OutType as ImplicitPromote>::Output>; + type Output = Array; fn neg(self) -> Self::Output { let cnst = constant(T::zero(), self.dims()); diff --git a/src/core/data.rs b/src/core/data.rs index dcca7acda..0fa0f0b94 100644 --- a/src/core/data.rs +++ b/src/core/data.rs @@ -124,7 +124,7 @@ extern "C" { /// - i16 /// - u16 /// -pub trait ConstGenerator { +pub trait ConstGenerator: HasAfEnum { /// The type of Array object returned by generate function type OutType: HasAfEnum; @@ -286,9 +286,9 @@ cnst!(u16, 11); /// # Return Values /// /// An Array of given dimensions with constant value -pub fn constant(cnst: G, dims: Dim4) -> Array +pub fn constant(cnst: T, dims: Dim4) -> Array where - G::OutType: HasAfEnum, + T: ConstGenerator, { cnst.generate(dims) } diff --git a/src/core/util.rs b/src/core/util.rs index 39cd4e219..f759dca72 100644 --- a/src/core/util.rs +++ b/src/core/util.rs @@ -424,7 +424,7 @@ impl From for RandomEngineType { /// This is an internal trait defined and implemented by ArrayFire /// create for rust's built-in types to figure out the data type /// binary operation's results. -pub trait ImplicitPromote { +pub trait ImplicitPromote: HasAfEnum { /// This type alias points to the type of the result obtained /// by performing a given binary option on given type and `RHS`. type Output: HasAfEnum; @@ -606,7 +606,7 @@ implicit!(bool, u16 => u16); implicit!(bool, u8 => u8); ///Trait qualifier to accept either real or complex typed data -pub trait FloatingPoint { +pub trait FloatingPoint: HasAfEnum { /// Use to check if trait implementor is real number fn is_real() -> bool { false @@ -639,19 +639,19 @@ impl FloatingPoint for f32 { } ///Trait qualifier to accept real data(numbers) -pub trait RealFloating {} +pub trait RealFloating: HasAfEnum {} impl RealFloating for f64 {} impl RealFloating for f32 {} ///Trait qualifier to accept complex data(numbers) -pub trait ComplexFloating {} +pub trait ComplexFloating: HasAfEnum {} impl ComplexFloating for c64 {} impl ComplexFloating for c32 {} ///Trait qualifier indicating it can hold real numbers only -pub trait RealNumber {} +pub trait RealNumber: HasAfEnum {} impl RealNumber for f64 {} impl RealNumber for f32 {} @@ -665,7 +665,7 @@ impl RealNumber for u64 {} impl RealNumber for i64 {} ///Trait qualifier for the type of Arrays accepted by scan operations -pub trait Scanable {} +pub trait Scanable: HasAfEnum {} impl Scanable for i32 {} impl Scanable for u32 {} @@ -674,7 +674,7 @@ impl Scanable for i64 {} /// Trait qualifier for type of Array's that are accepted /// by native image load/save functions. -pub trait ImageNativeType {} +pub trait ImageNativeType: HasAfEnum {} impl ImageNativeType for f32 {} impl ImageNativeType for u16 {} @@ -682,7 +682,7 @@ impl ImageNativeType for u8 {} /// Trait qualifier for type of Array's that are accepted /// by image processing functions especially filtering algorithms -pub trait ImageFilterType {} +pub trait ImageFilterType: HasAfEnum {} impl ImageFilterType for f64 {} impl ImageFilterType for f32 {} @@ -696,7 +696,7 @@ impl ImageFilterType for bool {} // TODO Rust haven't stabilized trait aliases yet /// Trait qualifier for given type indicating conversion capability between /// grayscale and RGB triplets of data -pub trait GrayRGBConvertible {} +pub trait GrayRGBConvertible: HasAfEnum {} impl GrayRGBConvertible for f64 {} impl GrayRGBConvertible for f32 {} @@ -708,7 +708,7 @@ impl GrayRGBConvertible for u8 {} // TODO Rust haven't stabilized trait aliases yet /// Trait qualifier for given type indicating computability of Moments -pub trait MomentsComputable {} +pub trait MomentsComputable: HasAfEnum {} impl MomentsComputable for f64 {} impl MomentsComputable for f32 {} @@ -720,7 +720,7 @@ impl MomentsComputable for u8 {} // TODO Rust haven't stabilized trait aliases yet /// Trait qualifier for given type indicating computability of Median -pub trait MedianComputable {} +pub trait MedianComputable: HasAfEnum {} impl MedianComputable for f64 {} impl MedianComputable for f32 {} @@ -733,7 +733,7 @@ impl MedianComputable for u8 {} // TODO Rust haven't stabilized trait aliases yet /// Trait qualifier for given type indicating if edge calculations such as /// derivates etc. can be performed -pub trait EdgeComputable {} +pub trait EdgeComputable: HasAfEnum {} impl EdgeComputable for f64 {} impl EdgeComputable for f32 {} @@ -744,7 +744,7 @@ impl EdgeComputable for u16 {} impl EdgeComputable for u8 {} /// Trait qualifier for given type indicating computability of covariance -pub trait CovarianceComputable {} +pub trait CovarianceComputable: HasAfEnum {} impl CovarianceComputable for f64 {} impl CovarianceComputable for f32 {} @@ -824,7 +824,7 @@ impl Fromf64 for u8 { fn fromf64(value: f64) -> Self { value as Self }} impl Fromf64 for bool { fn fromf64(value: f64) -> Self { value > 0.0 }} ///Trait qualifier for the type of Arrays accepted by scan operations -pub trait IndexableType {} +pub trait IndexableType: HasAfEnum {} impl IndexableType for f64 {} impl IndexableType for i64 {}