You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Recently, I ran into an unfamiliar visibility situation with a library that I used. I could not find any mentioning of this in the reference so I would like to know if this is just a (historic) artifact or actually by design. It concerns types in the signature of public functions which cannot be imported themselves.
Here is an example. Let's consider a workspace with the following layout:
// app/src/main.rsuse helper::get_tool;fnmain(){// Getting a `Tool` and debug-printing it workslet tool = get_tool();dbg!(tool);}// Not possible because `Tool` cannot be imported here.// fn modify_tool(t: Tool) -> Tool {// ...// }// error[E0603]: module `utils` is private// use helper::utils::Tool;// error[E0603]: struct `Tool` is private// use helper::Tool;
Described in words: We have a library helper which has a private submodule utils. The private submodule utils contains a public struct Tool which is used in the return type of the public function get_tool() -> Tool of the library. When using the library in the other crate app, we can call the public function get_tool() and obtain the type Tool which is public in a private module but leaked through the public function signature. However we cannot easily pass the type Tool across function boundaries because we cannot import it itself (there is probably some trickery possible with implicitly capturing the type in a closure, but not sure if we could still return it).
In the library where I found it, this was not intentional and the type was subsequently made public. However do some libraries use this intentionally? Should the visibility chapter of the reference include this advanced situation and some information when it makes sense to do so?
The text was updated successfully, but these errors were encountered:
Yes, sealed traits are often created by adding a super trait to the trait which can't be imported. This pattern is used in the standard library as well as in some other libraries.
mod sealed {pubtraitSealed{}}pubtraitSealedTrait: sealed::Sealed{}impl sealed::Sealedforu8{}implSealedTraitforu8{}
Ah thank you @bjorn3 for pointing out the use as sealed traits 🙏 Do you think it would be helpful to mention this in the respective chapter of the reference?
Recently, I ran into an unfamiliar visibility situation with a library that I used. I could not find any mentioning of this in the reference so I would like to know if this is just a (historic) artifact or actually by design. It concerns types in the signature of public functions which cannot be imported themselves.
Here is an example. Let's consider a workspace with the following layout:
Described in words: We have a library
helper
which has a private submoduleutils
. The private submoduleutils
contains a public structTool
which is used in the return type of the public functionget_tool() -> Tool
of the library. When using the library in the other crateapp
, we can call the public functionget_tool()
and obtain the typeTool
which is public in a private module but leaked through the public function signature. However we cannot easily pass the typeTool
across function boundaries because we cannot import it itself (there is probably some trickery possible with implicitly capturing the type in a closure, but not sure if we could still return it).In the library where I found it, this was not intentional and the type was subsequently made public. However do some libraries use this intentionally? Should the visibility chapter of the reference include this advanced situation and some information when it makes sense to do so?
The text was updated successfully, but these errors were encountered: