Types 对于每个实例都不同的关联类型

Types 对于每个实例都不同的关联类型,types,rust,Types,Rust,是否有一种方法可以指定rust中的关联类型,这些类型对于每个实例都是不同的?或者有没有计划将其引入语言?或任何其他实现此行为的方法?我认为scala有这个特性 例如,我需要这个特性来定义表示切片索引的类型,这些类型不需要进行范围检查。我不知道使用这个功能的语法,如果它存在的话。因此,我将使用我希望在下面的示例中使用的方法 // Each safe index has to implement this trait. pub trait SafeIndex { // Convert th

是否有一种方法可以指定rust中的关联类型,这些类型对于每个实例都是不同的?或者有没有计划将其引入语言?或任何其他实现此行为的方法?我认为scala有这个特性

例如,我需要这个特性来定义表示切片索引的类型,这些类型不需要进行范围检查。我不知道使用这个功能的语法,如果它存在的话。因此,我将使用我希望在下面的示例中使用的方法

// Each safe index has to implement this trait. 
pub trait SafeIndex {
    // Convert the safe index to an unsafe index.
    fn to_unsafe(&self) -> usize;
}

// Safe indexable
pub trait SafeIndexable {
    // The Item which is indexable.
    type Item;

    // In this context self.Index should be usable as index
    // without the need to do a range check.
    // This is a type alias which is distinct for each
    // instance. Therefore you need to specify an instance
    // (self) instead of a type name (Self) to access the type.
    // It is known, that each self.Index implements SafeIndex.
    type self.Index : SafeIndex;

    // Iterator of indices usable without range checking.
    type self.Indices : Iterator<Item = self.Index>;
    fn indices(&self) -> self.Indices;

    // Get item from index, without range checking.
    fn get<'s>(&'s self, index: self.Index) -> &'s Self::Item;
}

impl<I> SafeIndexable for &[I] {
    // The Item which is indexable.
    type Item = I;

    // This is a struct which is distinct for each
    // instance. Therefore you need to specify an instance
    // (self) instead of a type name (Self) to access the struct.
    struct self.IndexImpl {
        unsafe_index: usize,
    }
    impl SafeIndex for self.IndexImpl {
        fn to_unsafe(&self) -> usize {
            self.unsafe_index
        }
    }
    // This is the type alias which is distinct for each instance.
    type self.Index = self.IndexImpl;

    // Iterator of indices usable without range checking.
    struct self.IndicesImpl {
        begin: usize,
        end: usize,
    }
    impl Iterator for self.IndicesImpl {
        type Item = self.Index;
        fn next(&self) -> Option<Self::Item> {
            if self.begin != self.end {
                let cur = self.begin;
                self.begin += 1;
                Some(Self::Item{unsafe_index: cur})
            } else {
                None
            }
        }
    }
    type self.Indices = self.IndicesImpl;
    fn indices(&self) -> self.Indices {
        self.IndicesImpl{begin:0, end: self.len()}
    }

    // Get item from index, without range checking.
    fn get<'s>(&'s self, index: self.Index) -> &'s Self::Item {
        unsafe {
            let ptr = &self[0] as *const I;
            &*ptr.offset(index.unsafe_index as isize) as &Self::Item
        }
    }
}
//每个安全索引都必须实现此特性。
pub性状安全指数{
//将安全索引转换为不安全索引。
fn to_unsafe(&self)->使用;
}
//安全可转位
可转位的{
//可转位的项目。
类型项目;
//在此上下文中,self.Index应该可用作索引
//无需进行范围检查。
//这是一个类型别名,每个类型的别名都不同
//实例。因此,您需要指定一个实例
//(self)而不是类型名(self)来访问该类型。
//众所周知,每个self.Index都实现了SafeIndex。
类型self.Index:SafeIndex;
//无需范围检查即可使用的索引迭代器。
类型self.index:迭代器;
fn指数(&self)->自指数;
//从索引中获取项,不进行范围检查。
fn获取&的Self::Item;
}
可为&[I]执行安全索引{
//可转位的项目。
项目类型=I;
//这是一个结构,每个结构都是不同的
//实例。因此,您需要指定一个实例
//(self)而不是类型名(self)来访问结构。
结构self.IndexImpl{
不安全索引:使用,
}
用于self.IndexImpl的impl-SafeIndex{
fn to_unsafe(&self)->使用{
self.u索引
}
}
//这是对每个实例都不同的类型别名。
键入self.Index=self.IndexImpl;
//无需范围检查即可使用的索引迭代器。
结构自指示符号{
开始:使用,
完:usize,,
}
用于self.indicatesImpl的impl迭代器{
类型项=自索引;
fn下一步(&self)->选项{
如果self.begin!=self.end{
让cur=self.start;
self.begin+=1;
一些(Self::Item{unsafe_index:cur})
}否则{
没有一个
}
}
}
键入self.indicates=self.indicatesimpl;
fn索引(&self)->自索引{
self.indicatesImpl{begin:0,end:self.len()}
}
//从索引中获取项,不进行范围检查。
fn获取&的Self::Item{
不安全{
设ptr=&self[0]为*const I;
&*ptr.offset(index.unsafe_index as isize)as&Self::Item
}
}
}

这个特性有很多更有用的用例。例如,仅可生长的矢量。

也许你应该在rust站点上提出一个功能请求。我不知道你缺少哪项功能。。。这在rust中已经很容易做到了:您只需要在您的特性之外定义和实现“self.X”类型impl@ker这并不是每次都是唯一的类型。这允许,例如…@Pentagolo FWIW,你。我不认为你所要求的是可能的。啊,我现在明白了。。。你不能从一个变量中生成一个新类型。。。变量的生存期是唯一不同的东西,并且没有办法测试afaik生存期的等价性