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), and keeps fn main (the entry point must stay at the root);
  • 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;
  • a body-carrying mod (e.g. the test module) of any size is extracted whole to its own file — its attributes (#[cfg(test)] must keep gating it) and visibility travel to the root declaration, and its super::* keeps pointing at the root; one over the ceiling is split further into a {name}/ subdirectory.

Residual import/visibility that this mechanical scheme misses is left for the compiler to enumerate (the documented O(n) finish) — but the LOC budget and the byte-exact moves are done here.

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 pub(crate) use.

Functions§

reassemble_bin
Reassemble a binary crate-root split (back-compat wrapper).
split_bin
Plan and reassemble a binary crate-root split so that every output file is < max_loc. 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 so every output file is < max_loc; sub-modules see each other via super::*. 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.