Types 在包含nalgebra';s矢量撕裂型

Types 在包含nalgebra';s矢量撕裂型,types,rust,Types,Rust,我试图使用type来实现一些维度不可知的计算,但是在Copy特性周围我得到了一些奇怪的错误。下面的人为测试用例演示了该问题: extern crate nalgebra; use nalgebra::allocator::Allocator; use nalgebra::{DefaultAllocator, DimName, Real, VectorN}; #[derive(Clone, Debug, Copy, PartialEq)] pub struct LinearPathSegmen

我试图使用type来实现一些维度不可知的计算,但是在
Copy
特性周围我得到了一些奇怪的错误。下面的人为测试用例演示了该问题:

extern crate nalgebra;

use nalgebra::allocator::Allocator;
use nalgebra::{DefaultAllocator, DimName, Real, VectorN};

#[derive(Clone, Debug, Copy, PartialEq)]
pub struct LinearPathSegment<N: Real, D: DimName>
where
    DefaultAllocator: Allocator<N, D>,
{
    pub some_vec: VectorN<N, D>,
    pub some_scalar: N,
}

我确信
vectrain
确实实现了
Copy
;通过链
vectrain->MatrixMN->MatrixMN->Matrix
中的类型别名,我们可以看到,这意味着它也是为
vectrain
派生的。为什么编译器不这么说?我需要做什么才能使
vectrent
可复制?

在这种情况下,派生
Copy
是特殊的,因为
Matrix
是通用的:

#[repr(C)]
#[derive(Hash, Clone, Copy)]
pub struct Matrix<N: Scalar, R: Dim, C: Dim, S> {
    /// The data storage that contains all the matrix components and informations about its number
    /// of rows and column (if needed).
    pub data: S,

    _phantoms: PhantomData<(N, R, C)>,
}
作为一个简单的示例,此代码可以很好地编译:

#[derive(Clone, Copy)]
struct DummyWrapper<T>(T);

fn main() {
}

要使结构可复制,我只是猜测,但您应该添加以下边界:

N: Copy, D: Copy

您必须添加绑定的
Owned:Copy
。用作类型别名的一部分
Owned
最终成为的类型别名


嗯,
Real
所以这已经是正确的边界了。没有,但是添加带有
D:DimName+copy
的copy只会产生相同的错误。还有一个问题,我正在尝试使用
vectrat
,它不允许我在
R
C
s
上添加边界,因为它们被别名隐藏。你还有什么其他建议吗?@Bojangles不要使用别名,使用
Matrix
?也尝试了一下,还是没有骰子。这真让人困惑!
#[derive(Clone, Copy)]
struct DummyWrapper<T>(T);

fn consume<T>(_t: T) {}

fn main() {
    // Ok
    let a = DummyWrapper(42);
    consume(a);
    consume(a);

    // Error: move occurs because `a` has type `DummyWrapper<std::string::String>`,
    // which does not implement the `Copy` trait
    let a = DummyWrapper(String::from("test"));
    consume(a);
    consume(a);
}
N: Copy, D: Copy
extern crate nalgebra;

use nalgebra::{DefaultAllocator, DimName, Real, VectorN};
use nalgebra::allocator::Allocator;
use nalgebra::storage::Owned;

#[derive(Clone, Debug, Copy, PartialEq)]
pub struct LinearPathSegment<N: Real, D: DimName>
where
    DefaultAllocator: Allocator<N, D>,
    Owned<N, D>: Copy,
{
    pub some_vec: VectorN<N, D>,
    pub some_scalar: N,
}
impl<N, D> Copy for LinearPathSegment<N, D>
where
    N: Real,
    D: DimName,
    DefaultAllocator: Allocator<N, D>,
    VectorN<N, D>: Copy,
{
}