如何为简单的结构实现“Index”特征?

Gue*_*OCs 3 rust

我正在尝试实现Index一个简单特征的特征,并且我想将它与usize. 我添加了SliceIndex<[T], Output = T>这样我就可以用来T索引slice里面的A

use std::ops::Index;
use std::slice::SliceIndex;

struct A <'a, T>{
    slice: &'a [T]
}

impl<'a, T: Index<T, Output = T> + SliceIndex<[T], Output = T>> Index<T>
    for A<'a, T>
{
    type Output = T;

    #[inline(always)]
    fn index(&self, index: T) -> &Self::Output {
        self.slice.index(index)
    }
}

fn main() {
    let mut aa: Vec<u64> = vec![0; 10];
    let coefficient_iterable = A{slice: &aa};
    println!("{}", coefficient_iterable[1usize]);
}
Run Code Online (Sandbox Code Playgroud)

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9564b39061cae3e19db14217c10b9d8a

但我得到:

错误:

error[E0608]: cannot index into a value of type `A<'_, u64>`
  --> src/main.rs:22:20
   |
22 |     println!("{}", coefficient_iterable[1usize]);
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0608`.
error: could not compile `playground` due to previous error
Run Code Online (Sandbox Code Playgroud)

我不知道为什么,因为usize实现了SliceIndex<[T]>.

Sta*_*eur 6

要求T两者都是IndexSliceIndex是没有意义的,因为Index描述了可索引容器的行为,而SliceIndex是您可以对通用索引类型施加的限制,以使其更加灵活。

您不想实现,Index<T>因为T这里是切片中项目的类型,而不是索引的类型。为了使函数中的代码main正常工作,您只需实现Index<usize>,但一般实现Index<Idx>(where Idx: SliceIndex) 会提供更大的灵活性,因此您也可以使用范围。

use std::ops::Index;
use std::slice::SliceIndex;

struct A<'a, T> {
    slice: &'a [T],
}

impl<'a, T, Idx> Index<Idx> for A<'a, T>
where
    Idx: SliceIndex<[T], Output = T>,
{
    type Output = T;

    #[inline(always)]
    fn index(&self, index: Idx) -> &Self::Output {
        self.slice.index(index)
    }
}

fn main() {
    let aa: Vec<u64> = vec![0; 10];
    let coefficient_iterable = A { slice: &aa };
    assert_eq!(coefficient_iterable[1], 0);
}
Run Code Online (Sandbox Code Playgroud)