繁体   English   中英

Rust:向量实现问题

[英]Rust: vector implementation problems

我正在尝试用 rust 编写一个数学向量库,但是我遇到了一些我很想解决的问题。 我想解决两个问题:

  • [T]未实现Sized ”错误
  • 使Vecor<T>element密集

最小工作示例

#[derive(Debug)]
struct Vector<T> {
    elements: [T]
}

// multiple base trait implementations
impl<T> /* … */ for Vector<T> { }


// one of multiple macros
macro_rules! impl_vector_normal_arithmetic {
    ($(impl $trait:ident for Vector { $function:ident($symbol:tt) })+) => {
        $(
            //            ———————— Error: the trait `Sized` is not implemented for `[T]`
            //            vvvvvv
            impl<T : Num> $trait for Vector<T> {
                type Output = Self;
            
                #[inline(always)]
                fn $function(&self, other: Self) -> Self {
                    Self::new(
                        self.iter()
                            .zip(other.iter())
                            .map(|(&lhs, &rhs)| lhs $symbol rhs)
                            .collect()
                    )
                }
            }
        )+
    };
}

impl_vector_normal_arithmetic! {
    impl Add for Vector { add(+) }
    impl Sub for Vector { sub(-) }
    impl Mul for Vector { mul(*) }
    impl Div for Vector { div(/) }
}

[T]未实现Sized ”错误

我对向量的实现由一个通用向量定义组成,理论上,该定义应该能够包含无限大的向量。 这些向量只能在给定大小后实例化 - 这意味着使用数组。 然后使用宏来添加一些算术特征(Add、Sub、Div、Mul 等)。 然而,这会导致一个问题。 我无法实现这些函数,因为编译器告诉我[T]没有实现Sized特征。 我确实理解错误告诉我什么,但我知道如何告诉编译器大小无关紧要(因为函数不在乎)。 我该怎么做才能解决这个编译器错误?

使Vecor<T>elements密集

我的Vector<T>结构包含 / 可以由[T]类型的elements定义。 昨天,我正在阅读一些内存分配,并遇到了稀疏和密集的内存分配。 我想知道由 Rust 定义的数组是否是密集的(id est 所有项目在(虚拟)内存中按顺序分配),因为这就是我想要通过这些向量定义实现的目标。

这些向量只能在给定大小后实例化 - 这意味着使用数组。

但是你还没有写数组; 您已经编写了一个slice ,这意味着运行时选择的大小,而不是一般的大小选择。 [T]具有动态大小; 因此Vector<T>具有动态大小; 因此Vector<T>不能在堆栈上传递或返回,并且必须始终在某个指针之后进行处理。 这可能不是您想要的。

要声明一个数组,您必须编写类型[T; N] [T; N] ,而不是[T]

#[derive(Debug)]
struct Vector<T, const N: usize> {
    elements: [T; N]
}

这样, Vector将始终为Sized ,避免您询问的错误。 您需要以相同的方式使现有的通用代码在NT通用。 例如,这是Add一个实现:

impl<T: num_traits::NumAssign, const N: usize> Add for Vector<T, N> {
    type Output = Self;
    fn add(mut self, other: Self) -> Self {
        for (self_el, other_el) in
            self.elements.iter_mut().zip(other.elements.into_iter())
        {
            *self_el += other_el;
        }
        self
    }
}

(它使用循环而不是.collect()因为Iterator::collect()不能收集到数组中,因为迭代器通常没有静态已知的长度。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM