corez: The Rust Shim That Makes std::io Vanish

A tiny no_std compatibility layer with a big job: keep Zcash code portable, safe, and independent of brittle ecosystem dependencies.

8 min read • View on GitHub • More from zcash

A wide mechanical bridge spans two very different computing worlds, with one side suggesting a server runtime and the other a small embedded device. The same stream of I/O tokens crosses the bridge through a narrow central shim, showing how one API can survive across targets without changing shape.
corez keeps the public I/O surface steady while the backend changes under feature flags and target constraints.
Key Takeaways

corez is a shim, which is another way of saying it is designed to be missed. In a normal Rust project, you reach for std::io and move on. In Zcash’s world, that assumption has to survive servers, wallets, and constrained targets, so the crate keeps the familiar surface and swaps the implementation underneath.

The crate that disappears

The trick is simple to describe and easy to underestimate. When std is available, corez::io re-exports the standard library’s I/O module. When it is not, the crate supplies its own compatible module with the same mental model. Downstream code gets continuity instead of conditional chaos.

One import, three execution modes. The public API stays stable while the backing implementation changes with features and target environment.

That matters because portability is not just a build concern. It is a product constraint. If one codebase has to work in a roomy runtime and a stripped-down environment, the abstraction boundary has to be stable enough that every consumer can keep speaking the same language.

Why Zcash needed its own I/O boundary

The immediate reason is practical. The ecosystem had already shown how fragile a shared dependency could be when core2 was yanked. corez is a response to that kind of volatility. Instead of depending on a compatibility crate whose future is outside Zcash’s control, the project owns the layer itself.

Dimensionstd modealloc no_std modepure no_std mode
API surfaceMatches std::io closelyMatches the same core traitsSame traits, reduced runtime assumptions
Memory footprintDelegated to stdUses alloc for richer valuesLeans on core-only data
Error richnessStandard library styleCan carry more contextKeeps error state lean
Target environmentsServers and desktopsConstrained but alloc-capable systemsBare metal and the smallest targets
Safety modelDelegated to std internalsSafe Rust onlySafe Rust only

The point is not that embedded targets are glamorous. The point is that a large privacy system needs a dependable low-level boundary. Once that boundary becomes somebody else’s package, your portability story inherits their maintenance risk.

How the shim works

The repo is deliberately mirrored. src/lib.rs is the dispatch point. Under #[cfg(feature = "std")], it re-exports std::io. Otherwise it exposes the crate’s own io module. That module is split into focused files for traits, errors, cursor logic, and implementations for slices and vectors.

#[cfg(feature = "std")]
pub use std::io;

#[cfg(not(feature = "std"))]
pub mod io;

This is the cleanest kind of conditional compilation. The call site does not need to know which reality it is in. The import stays the same, the trait names stay the same, and the implementation becomes a build-time decision instead of a downstream rewrite.

A close-up mechanical valve regulates flow into a segmented container, with one branch remaining compact and bare while another branch contains layered internal parts. A pointer-like arm redirects the flow across a small gap, suggesting how cursor writes can advance, split, and fill memory safely.
The crate’s safest tricks live in the details: cursor writes, zero-fill behavior, and a memory model that avoids unsafe code.

The safety trade-off: no unsafe, no drama

The crate forbids unsafe code outright, which changes the engineering style. That constraint removes the usual escape hatches for performance tricks, so the implementation leans into predictability: explicit error handling, careful cursor movement, and buffer writes that behave the same way every time.

That choice shows up in vec_write and related logic. If a cursor jumps past the current end of a vector, the gap is zero-filled instead of being left to undefined memory behavior. That makes the crate easier to reason about, easier to audit, and less dependent on optimizations that only matter in edge cases.

Why the error type matters

The error story is the balancing act. In alloc-capable builds, the crate can preserve richer error context. In leaner modes, it keeps the type compact and available without pulling in more machinery than the target can support. The API stays recognizable, but the storage strategy scales down cleanly.

That split is the real shape of the library. corez is not trying to be a better std. It is trying to be the smallest possible compatibility layer that still feels native to the code that depends on it.

The hidden governance story

There is one more surprising layer in the repo: AGENTS.md. The project is written not just for human contributors, but for AI coding agents too. That signals a team that sees contribution process as part of supply-chain quality, not an afterthought.

That matters here because the crate exists to reduce dependency risk. The same logic applies to people and tools. If the project wants to stay stable, it has to make its rules legible to the next agent that touches the code, whether that agent is a person or a model.

What corez is really buying

QuestionAnswer
What does corez replace?A brittle external compatibility layer.
What does it preserve?The std::io mental model.
What does it add?Control over feature-gated I/O behavior across target classes.
What does it avoid?Unsafe shortcuts and hidden dependency risk.

corez is invisible by design, but it is doing real architectural work. It lets one codebase keep its I/O vocabulary while changing its runtime assumptions underneath. For a system like Zcash, that is not just convenience. It is resilience.