Skip to main content

Module reassemble

Module reassemble 

Source
Expand description

Tier 2 step 2 — reassemble a planned split into output module files.

Target: a binary/crate-root file (e.g. src/main.rs) whose items all live in one module and reference each other by bare name. Splitting them into sibling modules breaks those bare references, so the scheme is:

  • the crate root keeps the file preamble + imports, the source’s own re-exports (pub use ... — dropping or demoting one would narrow the crate’s public API) and content-less mod name; declarations verbatim, declares each part module, re-exports each part with a single glob whose visibility matches the part’s widest item (pub use when the part has a public item, else pub(crate) use, and nothing for an impl-only part), keeps local types with inherent impls verbatim (their private fields stay accessible to children), and keeps fn main (the entry point stays here);
  • each part file copies the imports it references by name (preamble stays at the root), does use crate::* to see its siblings via the root re-exports, and carries its items with pub(crate) visibility bumped on so the re-exports can see them;
  • unconditional body-carrying modules are extracted whole, with attributes and visibility on their declarations; large bodies are split recursively;
  • conditional modules keep their braced boundaries inline, and oversized leaves stay intact in their original module. An unmet budget never justifies dropping a declaration.

Residual import/visibility that this mechanical scheme misses is left for the compiler to enumerate (the documented O(n) finish). Unmet LOC budgets are reported alongside the preserved output.

Structs§

OutputFile
A file the split would write.
SplitOutput
The reassembled output plus the parts of the plan that could not be met.

Enums§

Topology
How the split file sits in the module tree — fixes the sibling path prefix and where sub-module files go. Re-export visibility is per part in both topologies: pub use when the part has a public item (preserving the crate’s public API), else no wider than the part’s items need.

Functions§

reassemble_bin
Reassemble a binary crate-root split (back-compat wrapper).
split_bin
Plan and reassemble a binary crate-root split targeting files < max_loc. Indivisible items and conditional modules stay in their original scope; consult still_oversized and max_loc() for unmet budget constraints. The item budget reserves the full header LOC and the per-part overhead; since a part now copies only the imports it references (a subset of the header), this reservation is conservative — a packed part stays safely under budget.
split_mod
Plan and reassemble a library module targeting files < max_loc; sub-modules see each other via super::*. Unmet budgets are reported without discarding declarations. Handles both a directory-owning foo/mod.rs (sub-modules become siblings) and a plain foo.rs file module (sub-modules go in a foo/ subdir). root_stem is the source file’s stem.