Skip to content

Commit 1dc4bfd

Browse files
committed
Arithmetic operations B @ A are now only implemented for owned storage B
Previously, operators like +, - etc, denoted by @ in general, were implemented so that they allowed ArrayViewMut as the left hand side operand. It would update the data in the view, and return the same view. This can be confusing, and was suggested by @SuperFluffy to be removed. We tentatively remove those implementations here. To work around B @ A not being allowed for a mutable view `B`, you can use B @= A instead, or the corresponding stable methods iadd, imul, etc. Please see the Arithmetic Operations overview in the `ArrayBase` docs for an easy to read overview. Fixes #83
1 parent 2da5bc7 commit 1dc4bfd

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

examples/life.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,16 @@ fn iterate(z: &mut Board, scratch: &mut Board) {
4040
// compute number of neighbors
4141
let mut neigh = scratch.view_mut();
4242
neigh.assign_scalar(&0);
43-
let neigh = neigh
44-
+ z.slice(s![0..-2, 0..-2])
45-
+ z.slice(s![0..-2, 1..-1])
46-
+ z.slice(s![0..-2, 2.. ])
43+
neigh.iadd(&z.slice(s![0..-2, 0..-2]));
44+
neigh.iadd(&z.slice(s![0..-2, 1..-1]));
45+
neigh.iadd(&z.slice(s![0..-2, 2.. ]));
4746

48-
+ z.slice(s![1..-1, 0..-2])
49-
+ z.slice(s![1..-1, 2.. ])
47+
neigh.iadd(&z.slice(s![1..-1, 0..-2]));
48+
neigh.iadd(&z.slice(s![1..-1, 2.. ]));
5049

51-
+ z.slice(s![2.. , 0..-2])
52-
+ z.slice(s![2.. , 1..-1])
53-
+ z.slice(s![2.. , 2.. ]);
50+
neigh.iadd(&z.slice(s![2.. , 0..-2]));
51+
neigh.iadd(&z.slice(s![2.. , 1..-1]));
52+
neigh.iadd(&z.slice(s![2.. , 2.. ]));
5453

5554
// birth where n = 3 and z[i] = 0,
5655
// survive where n = 2 || n = 3 and z[i] = 1

src/lib.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,17 @@ pub type Ixs = isize;
296296
///
297297
/// Since the trait implementations are hard to overview, here is a summary.
298298
///
299-
/// Let `A` be an array or view of any kind. Let `B` be a mutable
300-
/// array (that is, either `OwnedArray`, `RcArray`, or `ArrayViewMut`)
299+
/// Let `A` be an array or view of any kind. Let `B` be an array
300+
/// with owned storage (either `OwnedArray` or `RcArray`).
301+
/// Let `C` be an array with mutable data (either `OwnedArray`, `RcArray`
302+
/// or `ArrayViewMut`).
301303
/// The following combinations of operands
302304
/// are supported for an arbitrary binary operator denoted by `@`.
303305
///
304306
/// - `&A @ &A` which produces a new `OwnedArray`
305307
/// - `B @ A` which consumes `B`, updates it with the result, and returns it
306308
/// - `B @ &A` which consumes `B`, updates it with the result, and returns it
307-
/// - `B @= &A` which performs an arithmetic operation in place
309+
/// - `C @= &A` which performs an arithmetic operation in place
308310
/// (requires `features = "assign_ops"`)
309311
///
310312
/// The trait [`Scalar`](trait.Scalar.html) marks types that can be used in arithmetic
@@ -313,7 +315,7 @@ pub type Ixs = isize;
313315
///
314316
/// - `&A @ K` or `K @ &A` which produces a new `OwnedArray`
315317
/// - `B @ K` or `K @ B` which consumes `B`, updates it with the result and returns it
316-
/// - `B @= K` which performs an arithmetic operation in place
318+
/// - `C @= K` which performs an arithmetic operation in place
317319
/// (requires `features = "assign_ops"`)
318320
///
319321
/// ## Broadcasting
@@ -2549,12 +2551,14 @@ macro_rules! impl_binary_op(
25492551
/// between `self` and `rhs`,
25502552
/// and return the result (based on `self`).
25512553
///
2554+
/// `self` must be an `OwnedArray` or `RcArray`.
2555+
///
25522556
/// If their shapes disagree, `rhs` is broadcast to the shape of `self`.
25532557
///
25542558
/// **Panics** if broadcasting isn’t possible.
25552559
impl<A, S, S2, D, E> $trt<ArrayBase<S2, E>> for ArrayBase<S, D>
25562560
where A: Clone + $trt<A, Output=A>,
2557-
S: DataMut<Elem=A>,
2561+
S: DataOwned<Elem=A> + DataMut,
25582562
S2: Data<Elem=A>,
25592563
D: Dimension,
25602564
E: Dimension,
@@ -2616,9 +2620,11 @@ impl<'a, A, S, S2, D, E> $trt<&'a ArrayBase<S2, E>> for &'a ArrayBase<S, D>
26162620
#[doc=$doc]
26172621
/// between `self` and the scalar `x`,
26182622
/// and return the result (based on `self`).
2623+
///
2624+
/// `self` must be an `OwnedArray` or `RcArray`.
26192625
impl<A, S, D, B> $trt<B> for ArrayBase<S, D>
26202626
where A: Clone + $trt<B, Output=A>,
2621-
S: DataMut<Elem=A>,
2627+
S: DataOwned<Elem=A> + DataMut,
26222628
D: Dimension,
26232629
B: Clone + Scalar,
26242630
{

0 commit comments

Comments
 (0)