| Add a README b66da6b nandithebull 3h ago | 1 | # rustnim |
| 2 | |
| 3 | A Rust → Nim transpiler. It parses Rust with `syn` and emits Nim source that |
| 4 | behaves the same way — same stdout, same exit status — when both are built and |
| 5 | run. |
| 6 | |
| 7 | The rule the whole thing is organised around: **never approximate a semantic |
| 8 | you cannot represent.** If a construct doesn't map, `rustnim` says so and exits |
| 9 | non-zero. It never writes a file it can't stand behind. |
| 10 | |
| 11 | That rule comes from a real failure. We tried an existing transpiler that |
| 12 | advertises Rust support on the `base16ct` crate; it emitted an empty file and |
| 13 | exited 0. The write-up is in [`findings/`](findings/). |
| 14 | |
| 15 | ## Build and run |
| 16 | |
| 17 | ```bash |
| 18 | cargo build --release |
| 19 | ./target/release/rustnim input.rs -o out.nim |
| 20 | nim c out.nim |
| 21 | ``` |
| 22 | |
| 23 | Several inputs are concatenated into one Nim module, in the order given — Nim |
| 24 | has no per-file `mod`, so items are flattened and names must not collide: |
| 25 | |
| 26 | ```bash |
| 27 | rustnim src/lib.rs src/error.rs --cfg feature=alloc -o crate.nim |
| 28 | ``` |
| 29 | |
| 30 | ## What works |
| 31 | |
| 32 | Functions and `impl` methods, structs, enums (C-like and data-carrying), |
| Add generics; transpile the part of cosmic-theme that is reachable ff34e1b nandithebull 1h ago | 33 | `Option`/`Result` with `?`, generics, `saturating_*`/`checked_*`, |
| 34 | `let`/`let mut`, the integer and float operators at |
| Add a README b66da6b nandithebull 3h ago | 35 | exact widths, `as` casts, `if`/`while`/`loop`/`for`, `match` including binding |
| 36 | patterns, `Vec`/slices/arrays, type aliases, function-typed parameters |
| 37 | (`impl Fn(A) -> B`), multi-file input, `#[cfg]`, and `println!`/`format!` with |
| 38 | `{}`, `{:?}`, `{:x}`, `{:b}`, positional and inline-named arguments, and |
| 39 | zero/space padding. |
| 40 | |
| Correct the README where trait impls and iterators are listed as rejected 428f374 nandithebull 3h ago | 41 | Also: the formatting trait impls (`Display`, `Debug`, `LowerHex`, `UpperHex`, |
| 42 | `Binary`, `Octal`) and `From`; and slice iterators — `iter`, `iter_mut`, |
| 43 | `enumerate`, `zip`, `chunks_exact`, `chunks_exact_mut`, `windows` — resolved |
| 44 | into a single index loop where each binding is an lvalue into the original |
| 45 | container, so `*d = v` through `iter_mut()` reaches the caller's slice. |
| 46 | Borrowed slices are views, not copies, including as return types. |
| 47 | |
| Add display.rs and the alloc half: all of base16ct now goes through afb2a6e nandithebull 2h ago | 48 | Closures and `unsafe` blocks, `&str` as a borrowed view rather than an owned |
| 49 | copy, formatting impls whose `fmt` body writes repeatedly, and the |
| 50 | `assert!`/`assert_eq!`/`debug_assert*` family. Modules: pass the crate root first and each further file after it, |
| Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 2h ago | 51 | and items are scoped by module, so `lower::decode` and `mixed::decode` stay |
| 52 | distinct. |
| 53 | |
| Correct the README where trait impls and iterators are listed as rejected 428f374 nandithebull 3h ago | 54 | Rejected with a reason, rather than guessed at: `i128`/`u128`, generics, |
| Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 2h ago | 55 | `move` closures, trait impls other than the ones above, iterator adaptors with |
| 56 | no index-loop equivalent (`map`, `filter`, `take_while`), float→int casts, and |
| 57 | any standard-library method that isn't mapped. |
| Correct the README where trait impls and iterators are listed as rejected 428f374 nandithebull 3h ago | 58 | |
| 59 | ## base16ct |
| 60 | |
| Add display.rs and the alloc half: all of base16ct now goes through afb2a6e nandithebull 2h ago | 61 | **It goes through.** `tests/cases/026-base16ct-crate/` transpiles every source |
| 62 | file of base16ct 1.0.0 — `error.rs`, `lower.rs`, `upper.rs`, `mixed.rs`, |
| 63 | `display.rs` — byte-for-byte as published on crates.io, plus `lib.rs`'s |
| 64 | `decoded_len`, `encoded_len` and `decode_inner` verbatim, with the `alloc` |
| 65 | half enabled. Decoding and encoding are byte-identical to rustc's, including |
| 66 | `encode_str` (a closure over an `unsafe` block returning a borrowed `&str` |
| 67 | view of the bytes just written) and `HexDisplay`, whose `UpperHex` impl writes |
| 68 | once per byte into the formatter. |
| 69 | |
| 70 | That is the crate the transpiler in [`findings/`](findings/) emitted an empty |
| 71 | file for, while exiting 0. |
| Add a README b66da6b nandithebull 3h ago | 72 | |
| Prove byte-identity for base16ct by enumerating whole input domains 99b8376 nandithebull 2h ago | 73 | How strong is "byte-identical"? [`PROOF.md`](PROOF.md) answers that precisely: |
| 74 | exhaustive over every two-byte decode input, every two-byte encode input and |
| 75 | every single byte through `encode_str`/`HexDisplay`, plus a compositional |
| 76 | argument for longer inputs and 20,000 pseudorandom cases attacking it. |
| 77 | `cargo test --test proof` runs it — 151,463 cases, 9.1 MB of output, compared |
| 78 | byte for byte. |
| 79 | |
| Lower the `log` facade, and add explicit enum discriminants 12c0a01 nandithebull 25m ago | 80 | ## `bitflags!` and `log` |
| Lower `bitflags!` directly, checked against the real crate af6e50f nandithebull 43m ago | 81 | |
| 82 | `bitflags` is the most-depended-on translatable crate in libcosmic's tree (79 |
| 83 | of 741), and it is 26 `macro_rules!` definitions. Expanding the macro does not |
| 84 | help — the expansion still calls into the crate's own macro-defined runtime. |
| 85 | So the macro is lowered directly, and checked against the real crate: the test |
| 86 | links bitflags for the *oracle only*, and rustnim has to reproduce its |
| 87 | behaviour without it, byte for byte. |
| 88 | |
| Lower the `log` facade, and add explicit enum discriminants 12c0a01 nandithebull 25m ago | 89 | `log` (61 dependents) is the same shape — 20 `macro_rules!` — and gets the |
| 90 | same treatment. rustnim models log's *emitting* side: a transpiled library's |
| 91 | `info!` calls work and, with no logger installed, do nothing, exactly as in |
| 92 | Rust. Installing a logger is done from Nim with `rsLogSetLogger`. |
| 93 | |
| Lower `extern "C"` blocks to Nim importc; do not transpile libc 7db7991 nandithebull 15m ago | 94 | ## C interop |
| 95 | |
| 96 | An `extern "C"` block lowers to Nim `importc` declarations — both are |
| 97 | statements about a symbol someone else defines, bound by the same ABI. Raw |
| 98 | pointers map, and `*const T` becomes a const-qualified C declaration, because |
| 99 | Nim emits a real prototype where Rust emits none and the C compiler will |
| 100 | reject a mismatch. |
| 101 | |
| 102 | That is the answer to `libc`, which is *not* transpiled: it is 129,594 lines |
| 103 | of which 54,544 are constants and 7,660 are declarations, against 121 function |
| 104 | bodies in the whole crate. Nim reaches those symbols natively. |
| 105 | |
| Transpile a second crate, adler2, to test whether any of this generalises 5fdd5be nandithebull 2h ago | 106 | ## Does it generalise? |
| 107 | |
| 108 | `base16ct` is the crate this was built toward, so a second one was tried. |
| 109 | `adler2` 2.0.1 — a stateful checksum with operator-overload trait impls and a |
| 110 | hand-unrolled loop, structurally nothing like `base16ct` — works, and is |
| 111 | byte-identical across every single byte, every length to 600, and 144 |
| 112 | incremental-write splits. It needed ten new features, and it caught a |
| 113 | regression that 33 passing cases had not. |
| 114 | |
| Add generics; transpile the part of cosmic-theme that is reachable ff34e1b nandithebull 1h ago | 115 | `cosmic-theme`'s spacing scale, corner radii and density model also transpile |
| 116 | byte-identically — the part of it that is not built on `palette`. |
| 117 | |
| 118 | A 400-crate survey says something worth knowing: clearing the single most |
| 119 | common blocker has twice moved the number of fully-working crates by *zero*, |
| 120 | because each unblocked crate just hits its next one. Blockers are deep, not |
| 121 | wide, and a frequency ranking of first blockers is not a roadmap. |
| 122 | [`DESIGN.md`](DESIGN.md) has the tables. |
| Transpile a second crate, adler2, to test whether any of this generalises 5fdd5be nandithebull 2h ago | 123 | |
| Add a README b66da6b nandithebull 3h ago | 124 | ## Tests |
| 125 | |
| 126 | ```bash |
| 127 | cargo test |
| 128 | ``` |
| 129 | |
| 130 | The tests are differential, with rustc as the oracle. Each case in |
| 131 | `tests/cases/` is compiled by rustc and run, transpiled and compiled by Nim and |
| 132 | run, and the two stdouts and exit statuses must match. Building anything less |
| 133 | than that — checking the Nim output "looks right", say — is how you end up |
| 134 | shipping the bug in `findings/`. `rustnim` exiting 0 with a missing or empty |
| 135 | output file is a hard failure, and so is an empty corpus. |
| 136 | |
| 137 | rustc is invoked without `-O` so debug-profile overflow checks are on, which is |
| 138 | the matching pair for `nim c`'s defaults. |
| 139 | |
| 140 | Nim is found at `.nim-toolchain/bin/nim` in the repo root or any parent, or via |
| 141 | `RUSTNIM_NIM`. Run a single case with: |
| 142 | |
| 143 | ```bash |
| 144 | RUSTNIM_CASE=005 cargo test --test differential -- --nocapture |
| 145 | ``` |
| 146 | |
| 147 | ## Layout |
| 148 | |
| 149 | | file | role | |
| 150 | |---|---| |
| 151 | | `src/ty.rs` | Rust type → Nim type, exact widths, explicit rejections | |
| 152 | | `src/lower.rs` | items, statements, expressions → Nim | |
| 153 | | `src/fmt.rs` | `println!`/`format!` format strings | |
| 154 | | `src/prelude.nim` | `Option`/`Result`, panic, `Display`/`Debug` runtime | |
| 155 | | `tests/differential.rs` | the runner above | |
| 156 | |
| 157 | [`DESIGN.md`](DESIGN.md) has the reasoning: how each construct is mapped, what |
| 158 | was settled by running both compilers rather than by reading docs, and what's |
| 159 | still open. |