Skip to content

Commit 7d07769

Browse files
committed
Changed arith operator overloads
From now on, do the arith operations, users have to do the following ``` let a = randu(Dim4::new(&[4,4,1,1])); let b = &a + 3.5; let c = &a - &b; ``` We can't do just `a+b` because of move semantics limitation. This may be improved later when `Copy` trait can be implemented on Objects with `Drop` trait implemented.
1 parent d8684b7 commit 7d07769

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/arith/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ extern {
9292
fn af_isnan(out: MutAfArray, arr: AfArray) -> c_int;
9393
}
9494

95-
impl Not for Array {
95+
impl<'f> Not for &'f Array {
9696
type Output = Array;
9797

9898
fn not(self) -> Array {
@@ -190,7 +190,7 @@ binary_func!(pow, af_pow);
190190

191191
macro_rules! arith_scalar_func {
192192
($rust_type: ty, $op_name:ident, $fn_name: ident, $ffi_fn: ident) => (
193-
impl $op_name<$rust_type> for Array {
193+
impl<'f> $op_name<$rust_type> for &'f Array {
194194
type Output = Array;
195195

196196
fn $fn_name(self, rhs: $rust_type) -> Array {
@@ -228,10 +228,10 @@ arith_scalar_spec!(u8);
228228

229229
macro_rules! arith_func {
230230
($op_name:ident, $fn_name:ident, $ffi_fn: ident) => (
231-
impl $op_name<Array> for Array {
231+
impl<'f> $op_name<&'f Array> for &'f Array {
232232
type Output = Array;
233233

234-
fn $fn_name(self, rhs: Array) -> Array {
234+
fn $fn_name(self, rhs:&'f Array) -> Array {
235235
unsafe {
236236
let mut temp: i64 = 0;
237237
$ffi_fn(&mut temp as MutAfArray,

tests/hello_world.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ fn main() {
1616
af::print(&a);
1717

1818
println!("Element-wise arithmetic");
19-
let b: Array = af::sin(&a) + 1.5;
20-
af::print(&b);
21-
22-
let test = a.clone() + b.clone();
19+
let b: Array = &af::sin(&a) + 1.5;
20+
let b2: Array = &af::sin(&a) + &af::cos(&a);
21+
let b3: Array = ! &a;
22+
println!("sin(a) + 1.5 => "); af::print(&b);
23+
println!("sin(a) + cos(a) => "); af::print(&b2);
24+
println!("!a => "); af::print(&b3);
25+
26+
let test = &a + &b;
2327
af::print(&test);
2428

2529
// printf("Negate the first three elements of second column\n");

0 commit comments

Comments
 (0)