为什么特征实现只在同一个板条箱内发生冲突?

ajp*_*ajp 6 rust

此外,这还取决于特质的细节。

我有两个最小的板条箱。第一个是一个图书馆箱子foo,里面有

#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

pub trait KnownSize: Sized {
    const N: usize = core::mem::size_of::<Self>();
}
impl<T: Sized> KnownSize for T {}

pub trait SizeIs<const N: usize> {}
impl<T: KnownSize> SizeIs<{ T::N }> for T {}

struct X(u32);

// Uncommenting either of these lines breaks due to conflict with blanket impls above
// impl KnownSize for X {}
// impl SizeIs<4> for X {}
Run Code Online (Sandbox Code Playgroud)

在板条箱里bar,我有

use foo::*;

struct X(u32);

// Uncommenting this line breaks due to conflict with blanket impl in `foo`
// impl KnownSize for X {}

// However this line works! (and is required if I want to use the `X: SizeIs<4>` impl)
impl SizeIs<4> for X {}
Run Code Online (Sandbox Code Playgroud)
> rustc --version
rustc 1.76.0-nightly (6b771f6b5 2023-11-15)
Run Code Online (Sandbox Code Playgroud)