将特征方法和关联类型标记为专业化的默认值时预期输出类型发生变化
Expected output type changes when marking trait method and associated type as default for specialization
我想在 Rust 中为大多数 Rem
类型实现一个 modulo 操作:
#![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
}
}
没有 default
也能正常编译,但是有了 default
,我得到
error[E0308]: mismatched types
--> main.rs:
|
| default fn modulo(self, other: T) -> Self::Output {
| ------------ expected `<U as Modulo<T>>::Output` because of return type
| ((self % other.clone()) + other.clone()) % other
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected Modulo::Output, found std::ops::Rem::Output
|
= note: expected type `<U as Modulo<T>>::Output`
found type `<<<U as std::ops::Rem<T>>::Output as std::ops::Add<T>>::Output as std::ops::Rem<T>>::Output`
我不明白为什么会这样。我需要 default
s 因为我想将它专门用于 Copy
类型。
我每晚都在使用 Rust 1.29.0。
这里是问题的较小再现(MCVE):
#![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() {}
问题的出现是因为此实现的特化可以选择特化 Output
或 foo
,但不必两者都特化 :
impl<T> Example for T
where
T: Copy,
{
type Output = bool;
}
在这种情况下,foo
的原始实现将不再有意义 — 它不再返回 Self::Output
类型的值。
当前的专业化实施要求您同时考虑局部和全局,这是您必须在其中阅读错误消息的上下文。这并不理想,但像这样的问题(以及许多更复杂的事情,我当然)是它还不稳定的部分原因。
我想在 Rust 中为大多数 Rem
类型实现一个 modulo 操作:
#![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
}
}
没有 default
也能正常编译,但是有了 default
,我得到
error[E0308]: mismatched types
--> main.rs:
|
| default fn modulo(self, other: T) -> Self::Output {
| ------------ expected `<U as Modulo<T>>::Output` because of return type
| ((self % other.clone()) + other.clone()) % other
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected Modulo::Output, found std::ops::Rem::Output
|
= note: expected type `<U as Modulo<T>>::Output`
found type `<<<U as std::ops::Rem<T>>::Output as std::ops::Add<T>>::Output as std::ops::Rem<T>>::Output`
我不明白为什么会这样。我需要 default
s 因为我想将它专门用于 Copy
类型。
我每晚都在使用 Rust 1.29.0。
这里是问题的较小再现(MCVE):
#![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() {}
问题的出现是因为此实现的特化可以选择特化 Output
或 foo
,但不必两者都特化 :
impl<T> Example for T
where
T: Copy,
{
type Output = bool;
}
在这种情况下,foo
的原始实现将不再有意义 — 它不再返回 Self::Output
类型的值。
当前的专业化实施要求您同时考虑局部和全局,这是您必须在其中阅读错误消息的上下文。这并不理想,但像这样的问题(以及许多更复杂的事情,我当然)是它还不稳定的部分原因。