gpt4 book ai didi

types - 在包含 nalgebra 的 VectorN 类型的结构上派生 Copy 时出错

转载 作者:行者123 更新时间:2023-11-29 07:59:25 24 4
gpt4 key购买 nike

我正在尝试使用 nalgebra's VectorN 类型来实现一些与维度无关的计算,但我在 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,
}

Clone and run cargo build in this repo to reproduce

编译器(rustc 1.29.1 (b801ae664 2018-09-20))输出的错误是这样的:

error[E0204]: the trait `Copy` may not be implemented for this type
--> src/lib.rs:6:24
|
6 | #[derive(Clone, Debug, Copy, PartialEq)]
| ^^^^
...
11 | pub some_vec: VectorN<N, D>,
| --------------------------- this field does not implement `Copy`

我确定 VectorN执行Copy ;通过链中的类型别名 VectorN<...> -> MatrixMN<...> -> Matrix<...>我们看到 Copy is #[derive()] d on Matrix ,这应该意味着它是为 VectorN 派生的也。为什么编译器不这么说?我需要做什么才能制作 VectorN可复制?

最佳答案

您必须添加绑定(bind) Owned<N, D>: Copy . Owned 用作 MatrixMN 的一部分类型别名。 Owned最终成为 MatrixArray 的类型别名.

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,
}

Real 需要 Copy , 和 DimName 需要 Dim 这需要 Copy , 所以 ND不需要明确的 Copy边界。但出于某种原因,编译器无法证明 MatrixArrayCopy .我怀疑这来自绑定(bind) GenericArray<N, Prod<R::Value, C::Value>>: Copy在其 Copy实现。

另一种选择是添加绑定(bind) VectorN<N, D>: Copy .

请注意,任一选项都会强制对结构的每次使用都满足该限制。如果这不是您想要的,您必须为 Copy 编写一个手动实现。使用适当的边界而不是推导它。

impl<N, D> Copy for LinearPathSegment<N, D>
where
N: Real,
D: DimName,
DefaultAllocator: Allocator<N, D>,
VectorN<N, D>: Copy,
{
}

关于types - 在包含 nalgebra 的 VectorN 类型的结构上派生 Copy 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52650456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com