You can adapt the scope of the pub keyword: pub(in path), pub(crate), pub(super), and pub(self):
In addition to public and private, Rust allows users to declare an item as visible only within a given scope. The rules for pub restrictions are as follows:
pub(in path) makes an item visible within the provided path. path must be an ancestor module of the item whose visibility is > - being declared.
pub(crate) makes an item visible within the current crate.
pub(super) makes an item visible to the parent module. This is equivalent to pub(in super).
pub(self) makes an item visible to the current module. This is equivalent to pub(in self) or not using pub at all.
Incompatibility with workspace
I have the same problem as explained on this thread:
pub(crate) will limit the visibility to the crate, so it will be accessible by your crate, but not by the consumers of your crate
however, when you work with workspaces:
if you have some pub(crate) on the common crate, the cli crate won't be able to consume it (even if it is in your workspace)
if you put back pub, it will be available for everyone of your consumers
there is no pub(workspace) as a workspace is not a feature of the language but a feature of cargo
Proposed solution
Get rid of workspace ...
This is not a big project, so the compile time won't be a problem and I prefer not to expose private apis.
Adopted solution
The pub(crate) doesn't work, so like most crates, I added a #[doc(hidden)] annotation on private modules.
See implementation in #7
If you have a solution to make it work with workspace, please leave a comment.
This issue blocks #4
Workspace
At the time of writing this issue, the project is organized in workspaces:
As constraints for publishing, we have:
stream.rs
, the rest should be considered as private apis #4In a workspace, the submodules of the
common
crate need to bepub
in order to be consumed by thecli
crate.Visibility and Privacy
I found out about Visibility and Privacy:
You can adapt the scope of the
pub
keyword:pub(in path), pub(crate), pub(super), and pub(self)
:Incompatibility with workspace
I have the same problem as explained on this thread:
pub(crate)
will limit the visibility to the crate, so it will be accessible by your crate, but not by the consumers of your cratepub(crate)
on thecommon
crate, thecli
crate won't be able to consume it (even if it is in your workspace)pub
, it will be available for everyone of your consumerspub(workspace)
as a workspace is not a feature of the language but a feature ofcargo
Proposed solution
Get rid of workspace ...
This is not a big project, so the compile time won't be a problem and I prefer not to expose private apis.
Adopted solution
The pub(crate) doesn't work, so like most crates, I added a
#[doc(hidden)]
annotation on private modules.See implementation in #7
If you have a solution to make it work with workspace, please leave a comment.
Sources: