Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 将trait方法和关联类型标记为专门化的默认值时,预期输出类型会发生更改_Rust_Type Inference_Specialization - Fatal编程技术网

Rust 将trait方法和关联类型标记为专门化的默认值时,预期输出类型会发生更改

Rust 将trait方法和关联类型标记为专门化的默认值时,预期输出类型会发生更改,rust,type-inference,specialization,Rust,Type Inference,Specialization,我想为Rust中的大多数Rem类型实现一个操作: #![feature(specialization)] use std::ops::{Add, Rem}; /// Define a modulo operation, in the mathematical sense. /// This differs from Rem because the result is always non-negative. pub trait Modulo<T> { type Outpu

我想为Rust中的大多数
Rem
类型实现一个操作:

#![feature(specialization)]

use std::ops::{Add, Rem};

/// Define a modulo operation, in the mathematical sense.
/// This differs from Rem because the result is always non-negative.
pub trait Modulo<T> {
    type Output;

    #[inline]
    fn modulo(self, other: T) -> Self::Output;
}

/// Implement modulo operation for types that implement Rem, Add and Clone.
// Add and Clone are needed to shift the value by U if it is below zero.
impl<U, T> Modulo<T> for U
    where
        T: Clone,
        U: Rem<T>,
        <U as Rem<T>>::Output: Add<T>,
        <<U as Rem<T>>::Output as Add<T>>::Output: Rem<T>
    {
    default type Output = <<<U as Rem<T>>::Output as Add<T>>::Output as Rem<T>>::Output;

    #[inline]
    default fn modulo(self, other: T) -> Self::Output {
        ((self % other.clone()) + other.clone()) % other
    }
}
我不明白为什么会这样。我需要
默认值
s,因为我想将其专门用于
复制
类型


我每晚都在使用Rust 1.29.0。

这里是问题的一个较小的复制品(an):

在这种情况下,
foo
的原始实现将不再有意义-它不再返回类型为
Self::Output
的值


当前的专门化实现要求您同时考虑本地和全局,这是您必须阅读错误消息的上下文。这并不理想,但像这样的问题(我相信还有许多更复杂的事情)是它还不稳定的部分原因

FWIW,你可以说
U::Output:Add
-一个级别的deep不需要解决它的
@Shepmaster,但是我不知道从错误消息中得出的结论是什么
#![feature(specialization)]

trait Example {
    type Output;
    fn foo(&self) -> Self::Output;
}

impl<T> Example for T {
    default type Output = i32;

    default fn foo(&self) -> Self::Output {
        42
    }
}

fn main() {}
impl<T> Example for T
where
    T: Copy,
{
    type Output = bool;
}