Deriving Copy trait

Hi,

First of all, thanks for the great library!

I am trying to implement a data structure on top of a VectorN, but I struggle with deriving the Copy trait, because the b: VectorN<S, D> does not implement Copy.

As far as I can understand, however, this trait is implemented on https://github.com/rustsim/nalgebra/blob/master/src/base/matrix.rs#L75.

My (very dirty, sorry) project can be found at https://github.com/hovind/ad/tree/08146cf83151e842ae5ad08f80de0183f247169f.

Sorry for the poor description, this is my first project in rust, and I am struggling to get the grasp of it.

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Dual<S, D>
where
    S: Real,
    D: Dim,
    DefaultAllocator: Allocator<S, D>,
{
    a: S,
    b: VectorN<S, D>,
}

impl<S, D> Dual<S, D>
where
    S: Real,
    D: Dim + DimName,
    DefaultAllocator: Allocator<S, D>,
{
    pub fn new(x : VectorN<S, D>) -> VectorN<Dual<S, D>, D> {
        VectorN::<Dual<S, D>, D>::from_fn(|i : usize, _| -> Dual<S, D> {
            Dual {
                a: x[i],
                b: VectorN::<S, D>::from_fn(|j : usize, _| -> S {
                    if i == j {
                        S::one()
                    } else {
                        S::zero()
                    }
                }),
            }
        })
    }
}

A more minimal example:

extern crate nalgebra as na;
extern crate num_traits as num;
extern crate alga as al;

use std::f64;
use na::{DefaultAllocator, Dim, VectorN};
use na::allocator::Allocator;
use na::dimension::*;

fn main() {
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Dual<D>
where
    D : Dim + DimName + Copy,
    DefaultAllocator : Allocator<f64, D>
{
    a: f64,
    b: VectorN<f64, D>,
}

This yields:

       Fresh rand_core v0.3.0
       Fresh libm v0.1.2
       Fresh rawpointer v0.1.0
       Fresh libc v0.2.43
       Fresh rand_core v0.2.2
       Fresh num-traits v0.2.6
       Fresh typenum v1.10.0
       Fresh rand v0.5.5
       Fresh matrixmultiply v0.1.15
       Fresh approx v0.3.0
       Fresh num-complex v0.2.1
       Fresh generic-array v0.11.1
       Fresh alga v0.7.2
       Fresh nalgebra v0.16.8
   Compiling ad v0.1.0 (/home/hovind/Documents/rust/ad2)
     Running `rustc --crate-name ad src/main.rs --color never --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=0e48dc7260ef6e1c -C extra-filename=-0e48dc7260ef6e1c --out-dir /home/hovind/Documents/rust/ad2/target/debug/deps -C incremental=/home/hovind/Documents/rust/ad2/target/debug/incremental -L dependency=/home/hovind/Documents/rust/ad2/target/debug/deps --extern ad=/home/hovind/Documents/rust/ad2/target/debug/deps/libad-5946dd38927dcbf8.rlib --extern alga=/home/hovind/Documents/rust/ad2/target/debug/deps/libalga-cea341304e52f9b9.rlib --extern nalgebra=/home/hovind/Documents/rust/ad2/target/debug/deps/libnalgebra-06e546a306842046.rlib --extern num_traits=/home/hovind/Documents/rust/ad2/target/debug/deps/libnum_traits-eabc26a827e67f73.rlib`
error[E0204]: the trait `Copy` may not be implemented for this type
  --> src/main.rs:13:10
   |
13 | #[derive(Copy, Clone, Debug, PartialEq)]
   |          ^^^^
...
20 |     b: VectorN<f64, D>,
   |     ------------------ this field does not implement `Copy`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0204`.
error: Could not compile `ad`.

Caused by:
  process didn't exit successfully: `rustc --crate-name ad src/main.rs --color never --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=0e48dc7260ef6e1c -C extra-filename=-0e48dc7260ef6e1c --out-dir /home/hovind/Documents/rust/ad2/target/debug/deps -C incremental=/home/hovind/Documents/rust/ad2/target/debug/incremental -L dependency=/home/hovind/Documents/rust/ad2/target/debug/deps --extern ad=/home/hovind/Documents/rust/ad2/target/debug/deps/libad-5946dd38927dcbf8.rlib --extern alga=/home/hovind/Documents/rust/ad2/target/debug/deps/libalga-cea341304e52f9b9.rlib --extern nalgebra=/home/hovind/Documents/rust/ad2/target/debug/deps/libnalgebra-06e546a306842046.rlib --extern num_traits=/home/hovind/Documents/rust/ad2/target/debug/deps/libnum_traits-eabc26a827e67f73.rlib` (exit code: 1)

Add VectorN<f64, D>: Copy to your where clause.

1 Like