proxima-primitives 0.1.0

The pipe algebra itself: the Pipe trait — an async In -> Result<Out, Err> that becomes a transform, source, sink or observe by choice of types — plus its three tier siblings (SendPipe, UnpinPipe, UnpinSendPipe), Handler, SourcePipe, the payload types Carry, ResponseStream and RequestStream (buffered bodies are plain Bytes), HeaderList, the sans-IO byte-stream traits, runtime-agnostic concurrency primitives, and the Replay/GenericStream transport seam. Everything else in proxima composes what is defined here.
Documentation
//! Build-time tier wiring for proxima-primitives.
//!
//! Union of the former proxima-pipe and proxima-sync build scripts (Wave D
//! Phase 3 primitives merge). Three responsibilities, mirroring the proxima
//! sizing+profile build pattern (proxima-quic-proto.toml for caps,
//! proxima-time/build.rs for the profile):
//!
//! 1. Bake the no-alloc heapless cap from `proxima-primitives.toml` into
//!    `OUT_DIR/proxima_primitives_pipe_sized.rs` — always, regardless of
//!    profile. (from proxima-pipe/build.rs)
//! 2. Emit the `proxima_alloc` cfg that selects the `RetryRules` backing store:
//!    set => `alloc::collections::BTreeSet`; unset => the heapless const-cap
//!    form. When `PROXIMA_PROFILE` is set the cfg follows the resolved Profile's
//!    `alloc` axis (proxima-build, P8); otherwise it follows the cargo `alloc`
//!    feature so a plain `cargo build` keeps the BTreeSet form ("std as
//!    today"). (from proxima-pipe/build.rs)
//! 3. Declare the `loom` cfg so `--cfg loom` builds stay clean under
//!    `unexpected_cfgs`. (from proxima-sync/build.rs)

use std::env;
use std::fs;
use std::path::{Path, PathBuf};

use proxima_build::sizing::{SizingSource, require_nonzero};

/// Emits into two separate `OUT_DIR` files rather than one, because the two
/// consts are read under different cfgs: `RETRY_STATUS_CAP` only by the
/// no-alloc `RetryRules` backing store (dead under `proxima_alloc`, so its
/// `mod sized` stays `#[cfg(not(proxima_alloc))]`), while `FAN_IN_SOURCE_CAP`
/// is read unconditionally by `FanInVec` (its `mod fan_in_sized` has no cfg).
/// One shared file would force one shared cfg, making one const dead code
/// under the other's tier.
///
/// The env-var prefix (`PROXIMA_PIPE_`, not `PROXIMA_PRIMITIVES_`) is kept
/// verbatim as an unchanged contract even though the crate is now
/// `proxima-primitives` — this crate is the former proxima-pipe.
#[allow(clippy::expect_used)]
fn emit_sizing_consts(out_dir: &Path) {
    let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR set by cargo");
    let source = SizingSource::load(&manifest_dir, "proxima-primitives.toml", "PROXIMA_PIPE")
        .unwrap_or_else(|err| panic!("{err}"));

    let retry_status_cap = require_nonzero(
        "retry.status_cap",
        source
            .resolve_int("retry", "status_cap")
            .unwrap_or_else(|err| panic!("{err}")),
    );
    let fan_in_source_cap = require_nonzero(
        "fan_in.source_cap",
        source
            .resolve_int("fan_in", "source_cap")
            .unwrap_or_else(|err| panic!("{err}")),
    );

    let retry_out = format!(
        "// AUTO-GENERATED by build.rs from proxima-primitives.toml. DO NOT EDIT.\n\
         pub const RETRY_STATUS_CAP: usize = {retry_status_cap};\n",
    );
    let retry_out_path = out_dir.join("proxima_primitives_pipe_sized.rs");
    fs::write(&retry_out_path, retry_out)
        .unwrap_or_else(|err| panic!("write {}: {err}", retry_out_path.display()));

    let fan_in_out = format!(
        "// AUTO-GENERATED by build.rs from proxima-primitives.toml. DO NOT EDIT.\n\
         pub const FAN_IN_SOURCE_CAP: usize = {fan_in_source_cap};\n",
    );
    let fan_in_out_path = out_dir.join("proxima_primitives_pipe_fan_in_sized.rs");
    fs::write(&fan_in_out_path, fan_in_out)
        .unwrap_or_else(|err| panic!("write {}: {err}", fan_in_out_path.display()));
}

#[allow(clippy::expect_used)]
fn main() {
    let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR set by cargo"));

    // heapless cap const — always emitted; the no-alloc RetryRules reads it.
    emit_sizing_consts(&out_dir);

    // unconditionally, because the profile-absent branch below still sets
    // proxima_alloc from the cargo feature.
    proxima_build::emit_cfg_check_directives();

    // former proxima-sync/build.rs: declares `loom` so `--cfg loom` builds
    // (which never set this via a cargo feature) stay clean under
    // unexpected_cfgs.
    println!("cargo::rustc-check-cfg=cfg(loom)");

    // profile present => the Profile's alloc axis drives the cfg (proxima-build,
    // P8). absent => fall back to the cargo `alloc` feature so a plain build
    // keeps the BTreeSet form.
    if env::var("PROXIMA_PROFILE").is_ok() {
        let resolved = proxima_build::resolve_profile().expect("resolve proxima profile");
        proxima_build::emit_cfg_directives(&resolved);
        proxima_build::emit_generated_module(&resolved).expect("emit proxima_profile.rs");
        proxima_build::emit_rerun_directives(&resolved);
    } else if env::var("CARGO_FEATURE_ALLOC").is_ok() {
        println!("cargo:rustc-cfg=proxima_alloc");
    }
    println!("cargo:rerun-if-env-changed=PROXIMA_PROFILE");
}