Skip to content

el-ev/nichy

Repository files navigation

nichy

Rust type memory layout and niche optimization visualizer, powered by rustc internals.

nichy links directly against rustc via #![feature(rustc_private)] to query layout_of for every struct, enum, union, and type alias in your code. It reports exact sizes, alignments, field offsets, padding bytes, discriminant encoding, and niche optimization details. You get the same information the compiler uses, presented in a readable form.

Live instance: niche.rs

Or install locally:

curl --proto '=https' --tlsv1.2 -fsSL https://niche.rs/install.sh | sh

Usage

CLI

# analyze a type expression
nichy -t 'Option<&u64>'

# analyze a file
nichy src/types.rs

# pipe a snippet
echo 'struct Foo { a: u8, b: u64, c: u8 }' | nichy

JSON output for tooling:

nichy -t 'Result<u32, bool>' --json

Cross-target analysis:

nichy -t 'usize' --target aarch64-unknown-linux-gnu

Web

The web service serves a browser UI and a JSON API:

route purpose
POST /api/analyze Analyze {code, target} or {type, target}; returns layouts
GET /api/health return "ok"
POST /api/shorten Persist a snippet, returns a short id
GET /api/snippet/{id} Fetch a previously shortened snippet
GET /s/{id} Browser-loadable shortlink for a snippet
GET /api/stats / /stats Aggregate request stats (JSON / page)
GET /about Background reading on niche layouts
cargo run -p nichy-web    # serves on 127.0.0.1:3873

Configuration is in nichy-web.toml:

site_name = "niche.rs"
listen = ["0.0.0.0:3873"]
timeout_secs = 2.0
db_path = "nichy-web.db"

Install

install.sh writes a small launcher binary to ~/.local/bin/nichy. On each invocation the launcher reads rustc -V and runs a worker binary matched to that exact rustc release, downloading it to ~/.cache/nichy/ the first time. Any stable or beta toolchain on PATH works; rustup is the easy way to get one.

curl --proto '=https' --tlsv1.2 -fsSL https://niche.rs/install.sh | sh

Override the install location:

curl --proto '=https' --tlsv1.2 -fsSL https://niche.rs/install.sh \
    | NICHY_PREFIX=/usr/local sh

Building from source

You only need this path for local development on nichy itself, or to target a rustc release that's missing prebuilt artifacts. For everything else, prefer the install script above.

Local dev

git submodule update --init rust
cd rust
python3 x.py build library --stage 2 --target x86_64-unknown-linux-gnu
# build library for other targets you want to analyze
# python3 x.py build library --stage 2 \
#   --target aarch64-unknown-linux-gnu,i686-unknown-linux-gnu,wasm32-unknown-unknown
cd ..
cargo build -p nichy-cli -p nichy-web

Docker

Downloads prebuilt rustc tarballs from ci-artifacts.rust-lang.org.

DOCKER_BUILDKIT=1 docker build -f Dockerfile.ci-local \
    --build-arg RUST_VERSION=1.95.0 -t nichy-ci-local .

For rustc versions that aren't on ci-artifacts. Builds rustc from rust-lang/rust at the requested tag; takes tens of minutes or longer, depending on your machine.

docker build -f Dockerfile.rust-stable \
    --build-arg RUST_TAG=1.95.0 -t nichy-rust-stable:1.95.0 .
docker build -f Dockerfile.stable -t nichy-stable-test .

Web service

docker build -f Dockerfile.rust -t nichy-rust:main .   # once, or on rustc bump
docker build -t nichy-web .
docker run -p 3873:3873 nichy-web

License

MIT

About

nichy is a Rust type layout and niche optimization visualizer.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors