我想创建一个struct包含nalgebra::MatrixN这是U1更大的:
extern crate nalgebra as na;
use na::{DimName, DimNameAdd, DimNameSum, MatrixN, U1};
pub struct Homogenous<D: DimName>
where
D: DimNameAdd<U1>,
{
mat: na::MatrixN<f32, DimNameSum<D, U1>>,
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
extern crate nalgebra as na;
use na::{DimName, DimNameAdd, DimNameSum, MatrixN, U1};
pub struct Homogenous<D: DimName>
where
D: DimNameAdd<U1>,
{
mat: na::MatrixN<f32, DimNameSum<D, U1>>,
}
Run Code Online (Sandbox Code Playgroud)
尝试遵循错误消息会导致出现下一个特征错误消息的兔子洞。我看过 nalgebra 的 API,里面没有这么复杂的 trait 链。例如to_homogenous方法。我不确定我的方法是否正确。
也Dim有相应的特征DimAddand DimSum,但是由于该部分nalgebra没有真正记录下来,
我不知道我是否走在正确的道路上——或者我想做的事情是否可能。
这篇文章为我指明了正确的方向。这样做的方法nalgebra有点复杂:
extern crate nalgebra as na;
use crate::na::{Dim, DimName, DimNameAdd, DimNameSum, MatrixN, U1, DefaultAllocator};
use crate::na::allocator::Allocator;
pub struct Homogenous<D: Dim + DimName>
where
D: DimNameAdd<U1>,
DefaultAllocator: Allocator<f32, DimNameSum<D, U1>, DimNameSum<D, U1>>,
{
mat: MatrixN<f32, DimNameSum<D, U1>>,
}
Run Code Online (Sandbox Code Playgroud)
希望这些类型的通用操作在 Rust 和 nalgebra 的未来版本中变得更加符合人体工程学,因为这些相当繁琐的类型注释需要经常重复。
顺便说一句,仅将这些泛型类型存储在结构中仍然需要DefaultAllocator:
extern crate nalgebra as na;
use crate::na::{Dim, DimName, MatrixN, DefaultAllocator};
use crate::na::allocator::Allocator;
pub struct Homogenous<D: Dim + DimName>
where
DefaultAllocator: Allocator<f32, D, D>,
{
mat: MatrixN<f32, D>,
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
380 次 |
| 最近记录: |