rust - Is it possible to have a module which is partially accessible outside of a crate and partially only inside the crate? -


is there better way put in same module?

sub_module.rs

pub struct giantstruct { /* */ }  impl giantstruct {      // method needs called outside of 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 {         // stuff depending on fields of current         // giantstructbuilder     } } 

the problem giantstructbuilder::new_giant_struct(); method should create new giantstruct either need pub fn new() -> giantstruct inside of sub_module.rs or fields of giantstruct have public. both options allow access outside of crate.

while writing question, realized this:

sub_module.rs

pub struct giantstruct { /* */ }  impl giantstruct {     // can't call method without appropriate     // giantstructbuilder     pub fn new(&mut giantstructbuilder) -> giantstruct { /* */ };      pub fn do_stuff( /* */ ) { /* */ }; } 

however, seems counterintuitive caller thing acting while function variables is acted upon, not case when doing this. still know if there better way...

you use newly stabilized pub(restricted) privacy.

this allow expose types/functions limited module tree, e.g.

pub struct giantstruct { /* */ }  impl giantstruct {     // visible functions in same crate     pub(crate) fn new() -> giantstruct { /* */ };      // method needs called outside of crate.     pub fn do_stuff( /* */ ) { /* */ }; } 

or apply fields on giantstruct allow create giantstructbuilder:

pub struct giantstruct {      pub(crate) my_field: u32, } 

instead of crate use super specify it's public parent module.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -