Skip to content

Commit ed03341

Browse files
committed
Add missing bit shift trait impls for Array by unsigned integer types
1 parent b1b7c51 commit ed03341

File tree

1 file changed

+73
-2
lines changed

1 file changed

+73
-2
lines changed

src/core/arith.rs

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use num::Zero;
88

99
use libc::c_int;
1010
use num::Complex;
11+
use std::mem;
1112
use std::ops::Neg;
1213
use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Not, Rem, Shl, Shr, Sub};
1314

@@ -854,6 +855,49 @@ arith_func!(BitAnd, bitand, bitand);
854855
arith_func!(BitOr, bitor, bitor);
855856
arith_func!(BitXor, bitxor, bitxor);
856857

858+
macro_rules! bitshift_scalar_func {
859+
($rust_type: ty, $trait_name: ident, $op_name: ident) => {
860+
impl<T> $trait_name<$rust_type> for Array<T>
861+
where
862+
T: HasAfEnum + ImplicitPromote<$rust_type>,
863+
$rust_type: HasAfEnum + ImplicitPromote<T>,
864+
<T as ImplicitPromote<$rust_type>>::Output: HasAfEnum,
865+
{
866+
type Output = Array<<T as ImplicitPromote<$rust_type>>::Output>;
867+
868+
fn $op_name(self, rhs: $rust_type) -> Self::Output {
869+
let op2 = constant(rhs, self.dims());
870+
self.$op_name(op2)
871+
}
872+
}
873+
impl<'f, T> $trait_name<$rust_type> for &'f Array<T>
874+
where
875+
T: HasAfEnum + ImplicitPromote<$rust_type>,
876+
$rust_type: HasAfEnum + ImplicitPromote<T>,
877+
<T as ImplicitPromote<$rust_type>>::Output: HasAfEnum,
878+
{
879+
type Output = Array<<T as ImplicitPromote<$rust_type>>::Output>;
880+
881+
fn $op_name(self, rhs: $rust_type) -> Self::Output {
882+
let op2 = constant(rhs, self.dims());
883+
self.$op_name(op2)
884+
}
885+
}
886+
};
887+
}
888+
889+
macro_rules! shift_spec {
890+
($trait_name: ident, $op_name: ident) => {
891+
bitshift_scalar_func!(u64, $trait_name, $op_name);
892+
bitshift_scalar_func!(u32, $trait_name, $op_name);
893+
bitshift_scalar_func!(u16, $trait_name, $op_name);
894+
bitshift_scalar_func!(u8, $trait_name, $op_name);
895+
};
896+
}
897+
898+
shift_spec!(Shl, shl);
899+
shift_spec!(Shr, shr);
900+
857901
#[cfg(op_assign)]
858902
mod op_assign {
859903

@@ -871,7 +915,6 @@ mod op_assign {
871915
<A as ImplicitPromote<B>>::Output: HasAfEnum,
872916
<B as ImplicitPromote<A>>::Output: HasAfEnum,
873917
{
874-
#[allow(unused_variables)]
875918
fn $fn_name(&mut self, rhs: Array<B>) {
876919
let tmp_seq = Seq::<f32>::default();
877920
let mut idxrs = Indexer::default();
@@ -893,6 +936,35 @@ mod op_assign {
893936
arith_assign_func!(ShlAssign, shl_assign, shiftl);
894937
arith_assign_func!(ShrAssign, shr_assign, shiftr);
895938

939+
macro_rules! shift_assign_func {
940+
($rust_type:ty, $trait_name:ident, $op_name:ident, $func:ident) => {
941+
impl<T> $trait_name<$rust_type> for Array<T>
942+
where
943+
$rust_type: HasAfEnum + ImplicitPromote<T>,
944+
T: HasAfEnum
945+
+ ImplicitPromote<$rust_type>
946+
+ ImplicitPromote<$rust_type, Output = T>,
947+
{
948+
fn $op_name(&mut self, rhs: $rust_type) {
949+
let mut temp = $func(self, &rhs, false);
950+
mem::swap(self, &mut temp);
951+
}
952+
}
953+
};
954+
}
955+
956+
macro_rules! shift_assign_spec {
957+
($trait_name: ident, $op_name: ident, $func:ident) => {
958+
shift_assign_func!(u64, $trait_name, $op_name, $func);
959+
shift_assign_func!(u32, $trait_name, $op_name, $func);
960+
shift_assign_func!(u16, $trait_name, $op_name, $func);
961+
shift_assign_func!(u8, $trait_name, $op_name, $func);
962+
};
963+
}
964+
965+
shift_assign_spec!(ShlAssign, shl_assign, shiftl);
966+
shift_assign_spec!(ShrAssign, shr_assign, shiftr);
967+
896968
macro_rules! bit_assign_func {
897969
($op_name:ident, $fn_name:ident, $func: ident) => {
898970
impl<A, B> $op_name<Array<B>> for Array<A>
@@ -902,7 +974,6 @@ mod op_assign {
902974
<A as ImplicitPromote<B>>::Output: HasAfEnum,
903975
<B as ImplicitPromote<A>>::Output: HasAfEnum,
904976
{
905-
#[allow(unused_variables)]
906977
fn $fn_name(&mut self, rhs: Array<B>) {
907978
let tmp_seq = Seq::<f32>::default();
908979
let mut idxrs = Indexer::default();

0 commit comments

Comments
 (0)