Skip to content

Commit b56ec5d

Browse files
committed
Rename Scalar -> ScalarOperand, use it for A @= K
This makes the bounds on array-scalar arithmetic slightly stricter.
1 parent 56a04ea commit b56ec5d

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

src/lib.rs

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ extern crate num as libnum;
7474
use libnum::Float;
7575
use libnum::Complex;
7676

77+
use std::any::Any;
7778
use std::cmp;
7879
use std::mem;
7980
use std::ops::{Add, Sub, Mul, Div, Rem, Neg, Not, Shr, Shl,
@@ -352,7 +353,7 @@ pub type Ixs = isize;
352353
/// - `C @= &A` which performs an arithmetic operation in place
353354
/// (requires crate feature `"assign_ops"`)
354355
///
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
356357
/// with arrays directly. For a scalar `K` the following combinations of operands
357358
/// are supported (scalar can be on either side).
358359
///
@@ -2421,33 +2422,39 @@ impl_binary_op_inherent!(Shr, shr, ishr, ishr_scalar, "right shift");
24212422

24222423
/// Elements that can be used as direct operands in arithmetic with arrays.
24232424
///
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.
24252427
///
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.
24292431
///
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 `@`).
24342435
///
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
24362443
/// 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> { }
24512458

24522459
macro_rules! impl_binary_op(
24532460
($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>
25312538
where A: Clone + $trt<B, Output=A>,
25322539
S: DataOwned<Elem=A> + DataMut,
25332540
D: Dimension,
2534-
B: Clone + Scalar,
2541+
B: ScalarOperand,
25352542
{
25362543
type Output = ArrayBase<S, D>;
25372544
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>
25512558
where A: Clone + $trt<B, Output=A>,
25522559
S: Data<Elem=A>,
25532560
D: Dimension,
2554-
B: Clone + Scalar,
2561+
B: ScalarOperand,
25552562
{
25562563
type Output = OwnedArray<A, D>;
25572564
fn $mth(self, x: B) -> OwnedArray<A, D>
@@ -2721,13 +2728,12 @@ mod assign_ops {
27212728

27222729
#[doc=$doc]
27232730
/// **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>,
27262733
S: DataMut<Elem=A>,
27272734
D: Dimension,
2728-
B: Clone + Scalar,
27292735
{
2730-
fn $method(&mut self, rhs: B) {
2736+
fn $method(&mut self, rhs: A) {
27312737
self.unordered_foreach_mut(move |elt| {
27322738
elt.$method(rhs.clone());
27332739
});

0 commit comments

Comments
 (0)