rust - 如果从模块导出的项目暴露了内部代码,如何组织内部代码?

标签 rust module interface implementation

我正在开发一个 Rust 模块,该模块具有较小的外部接口(interface),但其内部实现又大又复杂。

是否有一种明智的方法可以避免使用包含模块所有代码的巨大文件,同时仍然向外部模块使用者公开一个小接口(interface)?

据我所知,Rust 不允许您对同一模块使用多个文件,例如 Go 允许使用包目录。 Rust 模块定义在一个目录中并不重要,它仍然需要一个文件。

这样,为了拆分代码,您必须使用子模块来表示您想要拆分的子类型或实现细节。据我所知,对于编译的代码来说应该没有多大关系,模块组织的所有开销都被删除了。

但是存在一个设计问题,如果我将内容拆分为模块,那么这些内容将需要导出内容,以便我可以从其他模块中使用它们...但是这些“导出的内部结构”也可以由外部使用消费者,对吗?有没有办法避免暴露这些内部接口(interface)?

最佳答案

But there is a design problem, if I split things up in modules, those will need to export things so that I can used them from my other modules... but then those "exported internals" can ALSO be used by external consumers, right?

不,他们不能。

在子模块中公开符号只会使其对其直接父模块可用。然后,该父模块必须通过 pub use 重新导出该符号,以获得“冒泡”的可见性。

mod foo {
    mod bar {
        pub fn visible_in_foo() {}
        pub fn visible_in_root() {}
        fn private() {}
    }
    
    // Re-export to make visible at the next outer level
    pub use bar::visible_in_root;

    pub fn foo() {
        // We are in the immediate parent, so both `pub` functions are visible
        bar::visible_in_foo();
        bar::visible_in_root();
        // bar::private(); - Won't compile
    }
}

// Make it public to consumers of the library, aka. external API
pub use foo::foo;

fn main() {
    // Notice that it's just `foo`. Not `foo::bar` or `bar`.
    // Re-exporting makes the symbol a part of module `foo`.
    foo::visible_in_root();

    // foo::visible_in_foo(); - Won't compile

    // `foo::foo` does have access to the inner module, though,
    // so we can call `foo::bar::visible_in_foo` indirectly here.
    foo::foo();
}

此外,您可以将符号标记为 pub(crate),其工作方式类似于 pub,只不过它可以防止符号在 crate 之外公开,即使它是由其所有父模块重新导出。

进一步阅读:

关于rust - 如果从模块导出的项目暴露了内部代码,如何组织内部代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74685528/

相关文章:

rust - 循环中的Rust所有权

drupal - 最好的 Drupal Survey 模块是什么

javascript - 依赖node.js模块状态可以吗?

java - 接口(interface)类型的实例变量无法访问实现类的方法,而实现类类型的实例变量可以访问

Rust 对迭代器中的引用的引用

reference - 当结构的一个实例需要引用另一个实例时,会出现生命周期问题

c# - "Program to an interface"使用扩展方法 : When does it go too far?

java - 戈朗 : what's the point of interfaces when you have multiple inheritence

rust - 我怎样才能拥有一个接受可变引用的属性,该引用将比其自身生命周期更长?

Python 子模块内部引用——它们真的很疯狂吗?