我有以下代码:
#![feature(on_unimplemented)]
use std::marker::PhantomData;
struct S<T>(PhantomData<T>);
#[rustc_on_unimplemented(message = "T1 missing")]
trait T1 {}
trait T2 {}
#[rustc_on_unimplemented(message = "T2 missing")]
impl<T: T1> T2 for S<T> {}
fn main() {
wants_t2(S::<u8>(PhantomData));
}
fn wants_t2<T: T2>(_: T) {}
编译时,出现此错误:
error[E0277]: T1 missing
--> src/main.rs:16:5
|
16 | wants_t2(S::<u8>(PhantomData));
| ^^^^^^^^ the trait `T1` is not implemented for `u8`
|
= note: required because of the requirements on the impl of `T2` for `S<u8>`
note: required by `wants_t2`
--> src/main.rs:19:1
|
19 | fn wants_t2<T: T2>(_: T) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^
寻找rustc_on_unimplemented
的实现时,是否有办法抑制T1
的{{1}}?某种程度上像Try
的方法过滤器?