Skip to content

feat(rust): port libobs/util/bitstream to Rust (four-tier tested) - #3

Merged
zackees merged 6 commits into
masterfrom
feat/rust-bitstream
Oct 8, 2026
Merged

zackees merged 6 commits into
masterfrom
feat/rust-bitstream

Conversation

@zackees

@zackees zackees commented Oct 8, 2026

Copy link
Copy Markdown
Member

Closes #1. Supersedes #2 (this branch contains the policy commit).

First Rust port: libobs/util/bitstream, done exactly per docs/rust-port/testing-policy.md. The public C API/ABI is unchanged; with ENABLE_RUST_LIBOBS=ON the C source is dropped and Rust provides the symbols.

What's here

  • Workspace: Cargo.toml, rust-toolchain.toml, rust/README.md.
  • rust/obs-util: safe core BitstreamReader + ffi::bitstream shim (#[repr(C)] struct matching the header). The core is generic over position: usize for the safe API, wrapping u8 for the C ABI. So the shim keeps C's uint8_t pos wrap without duplicating logic.
  • rust/obs-c-oracle: the original bitstream.c, unmodified, compiled with oracle_ symbols.
  • rust/libobs-rust: the single staticlib linked into libobs (one staticlib only, since each embeds its own std).
  • CMake: ENABLE_RUST_LIBOBS (default OFF) via Corrosion v0.5.2 with a whole-archive link. libobs/cmake/rust-exports.map hides Rust internals. ENABLE_UNIT_TESTS now actually adds test/cmocka (it was not wired up before).
  • rust/tools/linux-validate: a Docker harness for Tier 2.

Evidence

RED (commit ad02d8e, todo!() stubs):

test empty_buffer_reads_zero ... FAILED
test bitstream_test ... FAILED
test read_zero_bits_consumes_nothing ... FAILED
test read_bits_over_8_keeps_low_byte ... FAILED
test safe_core_does_not_wrap_past_byte_255 ... FAILED
test result: FAILED. 0 passed; 5 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

GREEN: soldr cargo test --workspace passes: Tier 1 has 5 tests, Tier 2 has the layout test, and Tier 3 has 3 parity tests. The parity tests are proptests against the C oracle with buffers up to 1024 bytes, arbitrary starting pos/subPos, and a deterministic run across the 255→0 wrap. clippy -D warnings, fmt --check, gersemi 0.25 and clang-format 22 are all clean.

Mutation checks:

  • Making the u8 position saturate instead of wrap fails both wrap-sensitive parity tests.
  • Replacing the C bit test (b & s) == s with != 0 fails matches_c_oracle_from_any_state.

Tier 2 (rust/tools/linux-validate/run.sh, Ubuntu 26.04, FFmpeg 8): builds libobs, the 4 cmocka tests and the decklink plugin in both configurations.

== [OFF] configure + build
== [OFF] ctest
100% tests passed, 0 tests failed out of 4
== [OFF] decklink.so imports:
                 U bitstream_reader_init
                 U bitstream_reader_r8
                 U bitstream_reader_read_bits
== [ON] configure + build
== [ON] ctest
100% tests passed, 0 tests failed out of 4
== [ON] decklink.so imports:
                 U bitstream_reader_init
                 U bitstream_reader_r8
                 U bitstream_reader_read_bits
== libobs exported symbols, OFF vs ON
IDENTICAL (1752 symbols)

With ON, util/bitstream.c is not compiled, and test/cmocka/test_bitstream.c and bitstream.h are unmodified.

Review: pre-push review found 2 LOW issues, both fixed in the last commit.

Known limits

  • Only Linux was validated. With the option OFF (the default) there is no change on any platform. Windows (obs.dll exports) and macOS (symbol hiding) are TODOs for when ON is enabled there.
  • No CI runs these tests yet, and per repo policy no workflow files were added.

Defines the four mandatory test tiers for porting libobs internals to Rust
while keeping the public C API/ABI and obs-websocket protocol compatible.
AGENTS.md/CLAUDE.md point every agent at the policy. The root .gitignore
allowlist gains the agent files and the Rust workspace paths.

Refs #1
Adds the Cargo workspace (rust-toolchain.toml, rust/obs-util,
rust/obs-c-oracle placeholder, rust/libobs-rust staticlib) and the Tier 1
tests from docs/rust-port/testing-policy.md. All tests fail against the
stubbed safe core.

Refs #1
- obs-util::ffi::bitstream: #[repr(C)] bitstream_reader matching the header
  and #[no_mangle] shims; the core is generic over position so the C ABI
  keeps the uint8_t pos wrap without duplicating logic.
- obs-c-oracle: original bitstream.c compiled with oracle_ symbols.
- Tier 2 layout test and Tier 3 proptest parity test (buffers up to 1024
  bytes, wrap past byte 255). Mutation check: breaking the u8 wrap fails
  both parity tests.

Refs #1
- libobs/cmake/rust.cmake: Corrosion v0.5.2 imports rust/libobs-rust and
  whole-archive links it; util/bitstream.c is dropped when ON.
- rust-exports.map hides Rust internals so libobs exports the same 1752
  symbols ON and OFF.
- ENABLE_UNIT_TESTS now actually adds test/cmocka (it was not wired up).
- rust/tools/linux-validate: Docker harness building OFF and ON, running the
  unchanged cmocka tests and diffing exported symbols.
- rust/README.md; testing policy updated with what this port established.

Closes #1
Review follow-up: the proptest now also starts from any (pos, subPos),
covering the subPos == 0 quirk (a mutation replacing the C bit test is
caught only by this case). The null-buf divergence from C (UB there) is
documented.

Refs #1
@zackees
zackees merged commit 694cff1 into master Oct 8, 2026
15 checks passed
@zackees
zackees deleted the feat/rust-bitstream branch October 8, 2026 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

feat(rust): port libobs/util/bitstream to Rust under the four-tier testing policy (first port)

1 participant