I have two types, each with a bunch of supporting functions; the entire content of these two types should be private (in my case, they are mutable pointers to objects in C). These two types are conceptually different, so I put them in different modules…
pub mod client {
pub struct Client {
// private internals
}
// Lots of code.
}
pub mod collection {
pub struct Collection {
// private internals
}
// Lots of code.
}
(In my real-world case, mod client
and mod collection
are separate files, i.e., client.rs
and collection.rs
.)
client.rs
needs to create a Collection
; however, it can't, because the internals are private. Any attempt to write a function suffers the same problem: the function would need to be in collection.rs
(to access the private members of Collection
, but would need to be pub
so that client.rs
can access it… but now it's also pub
to the entire world.
What I'd really like is some sort of crate-level visibility, or a way to "friend" this struct out to another module. (Otherwise, the pub
lically visible stuff in the crate doesn't represent the API, which seems silly.)
If you compare Rust and C++ you will notice that Rust does not have:
protected
friend
This is a deliberate design, and there is no alternative. In Rust something is either private or public, and therefore you do not have to jump all around the codebase to know whether an invariant holds or not.
Therefore, the solution is to:
pub fn new
function to create aCollection
This is actually good design; if there is one
struct
that should be responsible for upholdingCollection
's invariants, it certainly isCollection
itself. Delegating this responsibility to another crate seems hazardous.