简体   繁体   English

自动特征实现不起作用

[英]Automatic trait implementation does not work

On the code below, you can see that I forced the implementation of A for everything that implements AsRef<[T]> and Index[T] , so B should implement it, because it implements both of these.在下面的代码中,您可以看到我强制为实现AsRef<[T]>Index[T]的所有内容实现A ,因此B应该实现它,因为它实现了这两者。 However this is not the case.然而,这种情况并非如此。 Why?为什么?

use std::convert::{AsMut, AsRef};
use std::ops::{Index, IndexMut};
use std::slice::SliceIndex;

pub trait A<T, I: SliceIndex<[T], Output = T>>:
    AsRef<[T]> + Index<I, Output = T> 
{
    fn len(&self) -> usize;
}

impl<
        T: AsRef<[T]>  + Index<I, Output = T>,
        I: SliceIndex<[T], Output = T>,
    > A<T, I> for T
{
    fn len(&self) -> usize {
        self.as_ref().len()
    }
}

struct B<'a, T>{
    data: &'a mut [T]
}

impl<'a, T> AsRef<[T]> for B<'a, T> {
    fn as_ref(&self) -> &[T] {
        self.data
    }
}


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

    fn index(
        &self,
        v: I,
    ) -> &Self::Output {
        &self.as_ref()[v]
    }
}


fn something<'a, T>(r: &'a mut[T]) -> Box<dyn A<T, usize>> {
    let a = B{data: r};
    Box::new(a)
}

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

Error:错误:

error[E0277]: the trait bound `B<'_, T>: A<T, usize>` is not satisfied
  --> src/lib.rs:46:5
   |
46 |     Box::new(a)
   |     ^^^^^^^^^^^ the trait `A<T, usize>` is not implemented for `B<'_, T>`
   |
   = note: required for the cast to the object type `dyn A<T, usize, Output = T>`

You need to introduce an additional type parameter on the blanket impl for A :您需要在A的毯子 impl 上引入一个额外的类型参数:

impl<T, I, V> A<T, I> for V
where
    V: AsRef<[T]> + Index<I, Output = T>,
    I: SliceIndex<[T], Output = T>,
{
    fn len(&self) -> usize {
        self.as_ref().len()
    }
}

After fixing that, you'll get a lifetime error since the trait object returned in the box infers a 'static lifetime, so you'll need to bind it to the input slice's 'a lifetime.修复此问题后,您将收到生命周期错误,因为框中返回的特征 object 推断出'static生命周期”,因此您需要将其绑定到输入切片的'a生命周期”。

fn something<'a, T>(r: &'a mut [T]) -> Box<dyn A<T, usize> + 'a> {
    let a = B { data: r };
    Box::new(a)
}

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

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