代码之家  ›  专栏  ›  技术社区  ›  Lukas Kalbertodt

将私有类型移动到不同的模块中会导致“公共接口中的私有类型”

  •  1
  • Lukas Kalbertodt  · 技术社区  · 6 年前

    我有一个箱子,里面住着一个模块。出于内部目的,我想实施 ops::Index 对于具有两种私有索引和值类型的类型:

    pub mod inner {
        pub struct Data;
    
        struct Value;
        struct Key;
    
        impl std::ops::Index<Key> for Data {
            type Output = Value;
            fn index(&self, index: Key) -> &Self::Output {
                unimplemented!()
            }
        }
    }
    

    这很管用。到现在为止,一直都还不错。

    但是如果我改变 Key pub(crate) ,我得到这个错误( Playground ):

    error[E0446]: private type `inner::Value` in public interface
     --> src/lib.rs:9:9
      |
    4 |     struct Value;
      |     - `inner::Value` declared as private
    ...
    9 |         type Output = Value;
      |         ^^^^^^^^^^^^^^^^^^^^ can't leak private type
    

    我不明白这一点:我以为“private type in public interface”错误只是针对crate public interface。移动时也会发生同样的错误 钥匙 到另一个模块。

    为什么 impl block突然把 钥匙 酒吧(板条箱) ? 一个 执行 阻止决定?

    0 回复  |  直到 6 年前