Is there any better way than just put everything in the same module?
sub_module.rs
pub struct GiantStruct { /* */ }
impl GiantStruct {
// this method needs to be called from outside of the crate.
pub fn do_stuff( /* */ ) { /* */ };
}
lib.rs
pub mod sub_module;
use sub_module::GiantStruct;
pub struct GiantStructBuilder{ /* */ }
impl GiantStructBuilder{
pub fn new_giant_struct(&mut self) -> GiantStruct {
// Do stuff depending on the fields of the current
// GiantStructBuilder
}
}
The problem is with GiantStructBuilder::new_giant_struct()
; this method should create a new GiantStruct
but to do this you either need pub fn new() -> GiantStruct
inside of sub_module.rs
or all fields of GiantStruct
have to be public. Both options allow for access from outside of my crate.
While writing this question, I realized that I could do something like this:
sub_module.rs
pub struct GiantStruct { /* */ }
impl GiantStruct {
// now you can't call this method without an appropriate
// GiantStructBuilder
pub fn new(&mut GiantStructBuilder) -> GiantStruct { /* */ };
pub fn do_stuff( /* */ ) { /* */ };
}
However, this seems really counterintuitive as normally the caller is the thing that is acting while the function variables are what is is acted upon, which is obviously not the case when doing it like this. So I would still like to know if there is any better way...