@@ -74,6 +74,7 @@ extern crate num as libnum;
74
74
use libnum:: Float ;
75
75
use libnum:: Complex ;
76
76
77
+ use std:: any:: Any ;
77
78
use std:: cmp;
78
79
use std:: mem;
79
80
use std:: ops:: { Add , Sub , Mul , Div , Rem , Neg , Not , Shr , Shl ,
@@ -352,7 +353,7 @@ pub type Ixs = isize;
352
353
/// - `C @= &A` which performs an arithmetic operation in place
353
354
/// (requires crate feature `"assign_ops"`)
354
355
///
355
- /// The trait [`Scalar `](trait.Scalar .html) marks types that can be used in arithmetic
356
+ /// The trait [`ScalarOperand `](trait.ScalarOperand .html) marks types that can be used in arithmetic
356
357
/// with arrays directly. For a scalar `K` the following combinations of operands
357
358
/// are supported (scalar can be on either side).
358
359
///
@@ -2421,33 +2422,39 @@ impl_binary_op_inherent!(Shr, shr, ishr, ishr_scalar, "right shift");
2421
2422
2422
2423
/// Elements that can be used as direct operands in arithmetic with arrays.
2423
2424
///
2424
- /// This trait ***does not*** limit which elements can be stored in an `ArrayBase`.
2425
+ /// For example, `f64` is a `ScalarOperand` which means that for an array `a`,
2426
+ /// arithmetic like `a + 1.0`, and, `a * 2.`, and `a += 3.` are allowed.
2425
2427
///
2426
- /// `Scalar` simply determines which types are applicable for direct operator
2427
- /// overloading, e.g. `B @ K` or `B @= K` where `B` is a mutable array ,
2428
- /// `K` a scalar, and `@` arbitrary arithmetic operator that the scalar supports .
2428
+ /// In the description below, let `A` be an array or array view,
2429
+ /// let `B` be an array with owned data ,
2430
+ /// and let `C` be an array with mutable data .
2429
2431
///
2430
- /// Left hand side operands must instead be implemented one by one (it does not
2431
- /// involve the `Scalar` trait). Scalar left hand side operations: `K @ &A`
2432
- /// and `K @ B`, are implemented for the primitive numerical types and for
2433
- /// `Complex<f32>, Complex<f64>`.
2432
+ /// `ScalarOperand` determines for which scalars `K` operations `&A @ K`, and `B @ K`,
2433
+ /// and `C @= K` are defined, as **right hand side** operands, for applicable
2434
+ /// arithmetic operators (denoted `@`).
2434
2435
///
2435
- /// Non-`Scalar` types can still participate in arithmetic as array elements in
2436
+ /// **Left hand side** scalar operands are implemented differently
2437
+ /// (one `impl` per concrete scalar type); the are
2438
+ /// implemented for the default `ScalarOperand` types, allowing
2439
+ /// operations `K @ &A`, and `K @ B`.
2440
+ ///
2441
+ /// This trait ***does not*** limit which elements can be stored in an array in general.
2442
+ /// Non-`ScalarOperand` types can still participate in arithmetic as array elements in
2436
2443
/// in array-array operations.
2437
- pub trait Scalar { }
2438
- impl Scalar for bool { }
2439
- impl Scalar for i8 { }
2440
- impl Scalar for u8 { }
2441
- impl Scalar for i16 { }
2442
- impl Scalar for u16 { }
2443
- impl Scalar for i32 { }
2444
- impl Scalar for u32 { }
2445
- impl Scalar for i64 { }
2446
- impl Scalar for u64 { }
2447
- impl Scalar for f32 { }
2448
- impl Scalar for f64 { }
2449
- impl Scalar for Complex < f32 > { }
2450
- impl Scalar for Complex < f64 > { }
2444
+ pub trait ScalarOperand : Any + Clone { }
2445
+ impl ScalarOperand for bool { }
2446
+ impl ScalarOperand for i8 { }
2447
+ impl ScalarOperand for u8 { }
2448
+ impl ScalarOperand for i16 { }
2449
+ impl ScalarOperand for u16 { }
2450
+ impl ScalarOperand for i32 { }
2451
+ impl ScalarOperand for u32 { }
2452
+ impl ScalarOperand for i64 { }
2453
+ impl ScalarOperand for u64 { }
2454
+ impl ScalarOperand for f32 { }
2455
+ impl ScalarOperand for f64 { }
2456
+ impl ScalarOperand for Complex < f32 > { }
2457
+ impl ScalarOperand for Complex < f64 > { }
2451
2458
2452
2459
macro_rules! impl_binary_op(
2453
2460
( $trt: ident, $mth: ident, $imth: ident, $imth_scalar: ident, $doc: expr) => (
@@ -2531,7 +2538,7 @@ impl<A, S, D, B> $trt<B> for ArrayBase<S, D>
2531
2538
where A : Clone + $trt<B , Output =A >,
2532
2539
S : DataOwned <Elem =A > + DataMut ,
2533
2540
D : Dimension ,
2534
- B : Clone + Scalar ,
2541
+ B : ScalarOperand ,
2535
2542
{
2536
2543
type Output = ArrayBase <S , D >;
2537
2544
fn $mth ( mut self , x: B ) -> ArrayBase <S , D >
@@ -2551,7 +2558,7 @@ impl<'a, A, S, D, B> $trt<B> for &'a ArrayBase<S, D>
2551
2558
where A : Clone + $trt<B , Output =A >,
2552
2559
S : Data <Elem =A >,
2553
2560
D : Dimension ,
2554
- B : Clone + Scalar ,
2561
+ B : ScalarOperand ,
2555
2562
{
2556
2563
type Output = OwnedArray <A , D >;
2557
2564
fn $mth( self , x: B ) -> OwnedArray <A , D >
@@ -2721,13 +2728,12 @@ mod assign_ops {
2721
2728
2722
2729
#[ doc=$doc]
2723
2730
/// **Requires crate feature `"assign_ops"`**
2724
- impl <A , S , D , B > $trt<B > for ArrayBase <S , D >
2725
- where A : $trt<B >,
2731
+ impl <A , S , D > $trt<A > for ArrayBase <S , D >
2732
+ where A : ScalarOperand + $trt<A >,
2726
2733
S : DataMut <Elem =A >,
2727
2734
D : Dimension ,
2728
- B : Clone + Scalar ,
2729
2735
{
2730
- fn $method( & mut self , rhs: B ) {
2736
+ fn $method( & mut self , rhs: A ) {
2731
2737
self . unordered_foreach_mut( move |elt| {
2732
2738
elt. $method( rhs. clone( ) ) ;
2733
2739
} ) ;
0 commit comments