Lesson 7 of 9 · 25m
Structuring a Systems Crate That Can Grow Up
Separate policy, mechanism, platform glue, and public API so a crate remains testable as protocols, operating systems, and callers multiply.
A systems crate rarely dies because the first algorithm was wrong. It dies because the first folder layout made every later platform, protocol, and operational requirement squeeze through the same module. Good structure is not aesthetic. It determines whether you can test parsing without sockets, swap runtimes without rewriting policy, and audit unsafe code without reading the whole repository.
Separate policy from mechanism
Policy is what the system wants: retry twice, reject expired certificates, preserve ordering, rotate keys. Mechanism is how it happens: TCP, epoll, files, io_uring, OpenSSL, Windows APIs. Keep them apart:
src/
lib.rs public exports, narrow and deliberate
config.rs validated configuration types
protocol.rs wire parsing and serialization
engine.rs state machine and policy decisions
net/ socket and transport glue
platform/ OS-specific implementation details
error.rs actionable error taxonomyengine.rs should not import socket types. net/ should not decide business retry policy. That separation looks obvious on paper and is violated constantly in real services because the first prototype needed "just one more parameter."
Keep the public API narrow
lib.rs is a promise. Export types users need, not every internal convenience:
mod engine;
mod net;
mod protocol;
pub mod config;
pub mod error;
pub use engine::{Engine, EngineBuilder, Event};
pub use error::{Error, Result};Once a type is public, other people build on it. A private module lets you refactor internals without semantic-versioning pain. Rust's visibility system is one of your few defenses against accidental API surface.
State machines beat boolean soup
Systems crates manage lifecycles: disconnected, connecting, handshaking, ready, draining, closed. Represent state explicitly:
enum ConnectionState {
Connecting,
Ready { session_id: u64 },
Draining,
Closed,
}This prevents invalid combinations that booleans invite (connected=true, handshake_complete=false, closing=true). Rust enums are not just error machinery; they are architecture.
Platform code belongs behind narrow traits or modules
Do not scatter #[cfg] through core logic. Concentrate it:
#[cfg(unix)]
mod imp {
pub fn page_size() -> usize { unsafe { libc::sysconf(libc::_SC_PAGESIZE) as usize } }
}
#[cfg(windows)]
mod imp {
pub fn page_size() -> usize { 4096 } // simplified for illustration
}
pub use imp::page_size;Core logic calls page_size() and remains portable. Platform modules can be reviewed by people who understand those platforms.
Build examples as executable documentation
A small examples/probe.rs or examples/server.rs shows realistic setup better than documentation prose. It also catches API friction early: if using your crate requires twelve imports and three nested builders, the design is telling you something.
Field notes
- A
utils.rsmodule is usually a warning sign; after six months it becomes a junk drawer with circular dependencies. - Circular module dependencies mean ownership or policy boundaries were drawn incorrectly.
- Put parsing and state-machine tests in the core crate where they can run without privileged network access.
- Do not expose dependencies' types in public APIs unless you are prepared for those dependencies to become part of your compatibility contract.
Was this lesson helpful?