blob: 26b0d44237b250002063c636d8abf181c6b9add1 [file] [view]
# Exporting Types from Core
This document describes how visibility and re-exports work across Netstack3
Core's multi-crate architecture.
## Crate Boundaries and Build Visibility
Netstack3 Core is organized into two layers of crates under
`src/connectivity/network/netstack3/core/`:
- **Sub-crates**: Domain crates (`netstack3_device`, `netstack3_ip`,
`netstack3_tcp`, etc.) and foundational/support crates (`netstack3_base`,
`netstack3_sync`, etc.).
- **Top-level integration crate (`netstack3_core`)**: Located in `core/src/`,
this crate depends on all Core sub-crates, wires their context traits together
on `CoreCtx` and `StackState`, and exposes the single public API consumed by
Bindings.
Build-level `visibility` rules in `BUILD.bazel` enforce this layering:
- Bindings (`//src/connectivity/network/netstack3`) depends only on
`netstack3-core`.
- Leaf domain crates (e.g. `netstack3_device`) restrict production visibility to
`//src/connectivity/network/netstack3/core:__pkg__`.
- Shared lower-layer domain and support crates (e.g. `netstack3_base`) restrict
visibility to `//src/connectivity/network/netstack3/core:__subpackages__`.
As a result, Bindings cannot import Core sub-crates directly. Making an item
`pub` inside a sub-crate exposes it to other Core crates, *not* to Bindings.
## Exporting Items from Core Sub-crates
Each Core sub-crate controls its own crate boundary using private implementation
modules, `pub` vs. `pub(crate)` visibility modifiers, and explicit re-exports in
`src/lib.rs`.
### Private Implementation Modules (`mod internal`)
Domain sub-crates (e.g. `netstack3_udp`) wrap their source modules inside a
private `internal` module in `src/lib.rs`:
```rust
#[path = "."]
mod internal {
pub(super) mod base;
pub(super) mod counters;
pub(super) mod diagnostics;
}
```
This pattern provides three benefits:
- `#[path = "."]` keeps source files directly under `src/` rather than nested in
a `src/internal/` directory.
- Internal module names cannot collide with curated public re-export modules in
`src/lib.rs` (for example, `internal::device` vs. `pub mod device` in
`netstack3_ip`, or `internal::ethernet` vs. `pub mod ethernet` in
`netstack3_device`).
- Downstream crates cannot depend on internal file layout, so files under
`internal` can be reorganized without breaking other Core crates.
### `pub` vs. `pub(crate)` Within a Crate
Because implementation modules are private, Rust allows marking an item `pub`
even if it is never re-exported from `src/lib.rs`. Within each crate, follow
this rule to keep visibility clear and compiler-checked:
**Mark items shared only within the same crate as `pub(crate)` (or `pub(super)`),
and mark an item `pub` if it is re-exported (or referenced by a symbol that is
re-exported) from that crate's `src/lib.rs`.**
### Sub-crate Re-exports
Sub-crates explicitly `pub use` exported items in `src/lib.rs`, either at the
crate root or grouped into curated `pub mod` namespaces (such as
`netstack3_ip::{device, gmp, icmp, multicast_forwarding, nud, raw, socket}` or
`netstack3_device::{blackhole, ethernet, loopback, pure_ip, queue, socket}`).
## Exporting the Bindings-Facing API from `netstack3_core`
The top-level `netstack3_core` crate (`core/src/lib.rs`) acts as the curated
facade for Bindings:
- **Integration modules stay internal**: Private and `pub(crate)` wiring modules
implement each sub-crate's context traits on `CoreCtx` and `StackState`.
- **Curated domain modules**: Types, errors, events, and bindings traits needed
by Bindings are explicitly `pub use`d into flat domain modules in
`core/src/lib.rs` (e.g. `netstack3_core::tcp`).
- **Root-level entry points**: Top-level orchestration types and foundational
traits (e.g. `StackState`) are re-exported directly at the crate root.
- **Imperative operations via `CoreApi`**: Bindings calls into Core through
`CoreApi` (obtained from `StackState::api`), whose methods return each
sub-crate's `*Api` handle bound to `CoreCtx` (e.g. `CoreApi::tcp`). Most
`*Api` structs do not need to be re-exported by name because Bindings calls
methods directly on the returned handle.
## Exporting Test Utilities
Test-only types and helpers that need to be shared across Core crates or with
Bindings tests are gated behind `#[cfg(any(test, feature = "testutils"))]` and
re-exported from `pub mod testutil` in each crate's `src/lib.rs` (e.g.
`netstack3_base::testutil`).
Some Core crates define dedicated build variants (`-testutils`, `-instrumented`,
`-benchmarks`, `-loom`) alongside their default library target, and
`core/BUILD.bazel` uses `assert_no_deps` tags to verify that production targets
never depend on `-testutils` (or mismatched) sub-crate variants.
## Summary: Where to Export a New Type
1. **Used only within one Core crate**: Keep it in `internal` (or another
private module) with private, `pub(super)`, or `pub(crate)` visibility, and
do not re-export it in `src/lib.rs`.
2. **Used by sibling Core crates or `netstack3_core` integration code**: Mark it
`pub` inside the defining crate's private module, document it, and `pub use`
it from that sub-crate's `src/lib.rs`.
3. **Used by Bindings**: Export it from its defining sub-crate (step 2), then
explicitly `pub use` it into the appropriate domain module (or root) of
`core/src/lib.rs`.