- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 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
, 所以 N
和 D
不需要明确的 Copy
边界。但出于某种原因,编译器无法证明 MatrixArray
是Copy
.我怀疑这来自绑定(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/
我是一名优秀的程序员,十分优秀!