Skip to content

obs-util: Add a Rust equivalent of util/deque.h - #1561

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/deque
Oct 20, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/deque

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

Refs #1546 (step 3, part 2 of 2; part 1, #1548, added the characterization test test_deque)

What and why

The obs-output delay port (#1546, step 4) keeps output->delay_data, a C struct deque that obs-output.c initializes and frees. It has to push to, peek and pop that deque from Rust with the same semantics as the C inlines. libobs/util/deque.h is header-inline, so nothing swaps: like darray.h, the Rust equivalent is a library that ports call, and the struct layout is the contract (testing-policy.md, "Header-inline code"). The C inlines and libobs are unchanged.

  • Safe core (rust/obs-util/src/deque.rs):
    • Deque<B: DequeBuf = VecBuf> holds the four size fields (DequeState) and a byte buffer. DequeBuf is brealloc / bfree plus the bytes, so the same code runs on a Vec (VecBuf) and on a C deque's bmalloc memory.
    • It follows the C step for step:
      • the growth rule: the first push allocates exactly its size, then the capacity doubles, or becomes the new size when that is larger;
      • the wrap points;
      • end_pos == capacity when the deque is full and unwrapped (characterized, not endorsed);
      • the move of the wrapped tail on growth and on reserve;
      • the reset to position 0 when the deque empties.
    • upsize is push_back_zero of the difference, as in the C body. A null pop destination is discard_front / discard_back. Peek and pop past the size panic, where C asserts.
    • No host cfg, no unsafe.
  • Raw API (rust/obs-util/src/ffi/deque.rs):
    • the #[repr(C)] struct deque mirror and the C functions with their C signatures (deque_init, deque_free, deque_reserve, deque_upsize, deque_place, deque_push_back / _front / _back_zero / _front_zero, deque_peek_front / _back, deque_pop_front / _back, deque_data) as plain unsafe fns.
    • Each one runs the safe core in place on the C struct and writes the fields back through a drop guard. The buffer comes from the bmem port (brealloc / bfree, through ffi/darray.rs), so C's deque_free frees what Rust allocated, and Rust grows and frees what C allocated.
    • Nothing is exported (no #[no_mangle]), so rust-exports.d, rust-hidden.txt and the export maps are unchanged.
    • deque_reorder_data and deque_ensure_capacity are internal steps of the other functions. They are not exposed on their own.
  • Oracle (rust/obs-c-oracle/oracle/deque.c, src/deque.rs): non-inline oracle_deque_* wrappers of every inline function above, plus sizeof, _Alignof and every field offset of struct deque. Like darray.c, it includes the real header.
  • Intentional differences: none.
  • Docs: a rust/README.md row, and util/deque.h added to the header-inline list in testing-policy.md. naming.md needs no change: libobs/util/ already maps to obs-util.

test_deque (16 cases) is unchanged. The functions are static inline, so it tests the C header in both libobs builds and cannot see the Rust side. Tier 3 is the proof.

RED → GREEN

  • RED commit: 73284231a obs-util: Add Tier 1 tests for the util/deque.h equivalent. The safe core's methods are todo!(). soldr cargo test -p obs-util --lib tests::containers::deque fails 27 of 27 with "not yet implemented"; the two should_panic cases fail on the wrong panic message.
    • Tier 1 (tests/containers/deque.rs) ports every test_deque case except test_layout 1:1, under the same names without test_. test_layout is mirrored by the layout test. The bnum_allocs() checks count through a test buffer that counts like bmem, so they need no process-wide counter.
    • 12 new edge cases: zero-size operations on an empty deque (no allocation), a zero-size place past the end, a push into the gap at the front of a full, unwrapped deque, pop and peek across the wrap, a pop at the back to empty, reserve on an unwrapped offset and on an empty deque that has a buffer, place across the end inside the size, the two size checks, and VecBuf::free.
    • The tests are a module of the existing obs-util harness (containers category). No new test binary.
  • GREEN: b640fbc88 obs-util: Add a Rust equivalent of util/deque.h. tests::containers::deque passes 33/33: 27 Tier 1, 2 layout and 4 Tier 3.
    • Tier 2 (deque_layout.rs): size, alignment and every field offset against the oracle, plus the test_layout assertions (five pointer-sized fields in order, pointer alignment).
    • Tier 3 (deque_parity.rs):
      • deque_ops_match_c: a proptest over random sequences of up to 64 operations on three twins (the Rust raw functions, the C oracle and the safe core over a Vec). The operations are every push, pop with and without a destination, peek, reserve, upsize, place, deque_data (in range, past the size, and SIZE_MAX) and free. After every step it compares the four fields, whether the buffer is null, every live byte at its buffer offset, and each peek, pop and deque_data result, with a guard byte that catches an overlong copy. Bytes outside the live range are not compared, because C leaves grown memory uninitialized.
      • deque_mixed_in_place_matches_c: one C struct deque that each step hands to the Rust or the C functions at random, deque_free included, against a C-only twin. This is the obs-output case.
      • record_fifo_matches_c: the test_deque record FIFO (127 pushes, 96 pops, growth to 1024 bytes) run the same mixed way.
      • bmem_accounting_matches_c: the bnum_allocs() deltas through the Rust functions, run in its own process (common::isolated, Flaky: tests that need process-wide isolation race since #978 merged them into shared binaries #1133). The first push allocates and growth does not, popping to empty keeps the block, and deque_free releases it, whether C or Rust allocated it.
  • Deliberate-break check, in the Rust core, then restored:
    • Growth rule changed from max(size) to max(size + 1): 19 of 27 Tier 1 tests and all 4 Tier 3 tests failed, and the proptests shrank to a single PushBack.
    • push_back wrap condition changed from new_end_pos > capacity to >=: all 4 Tier 3 tests failed.
    • After the restore, 33/33 pass. The proptest regression file from the breaks was not kept.
  • Local, two build trees (ENABLE_RUST_LIBOBS=OFF and ON). Each runs the cmocka suite against libobs (OFF) and libobs-with-rust (ON), headless (private Xvfb, private D-Bus, fresh HOME):
Lane libobs libobs-with-rust Exported symbols
C (ENABLE_RUST_LIBOBS=OFF) 335/335 335/335 IDENTICAL (1766, same as master)
Rust (ENABLE_RUST_LIBOBS=ON) 335/335 335/335 IDENTICAL (1766)
  • No test was skipped or not run in any of the four runs, and the export lists are identical across the two lanes. test_deque passes 16/16 in all four (it tests the C header, so this shows only that nothing around it moved).
  • ci/local_gate.py:
    • --lane lint (clang-format, gersemi, rustfmt, clippy -D warnings, Dylints, ifdef ratchet, test-binary cap): passed.
    • --lane cross (clippy and Dylints for x86_64-pc-windows-msvc and aarch64-apple-darwin, plus a link of every test binary, the oracle included): passed.
    • --lane cross-release: passed.
    • --lane rust-test (soldr cargo test --workspace --locked, plus a --no-run link for x86_64-pc-windows-gnu): passed, 1670 tests, none failed. The obs-util harness passes 706/706 (673 before, plus the 33 here). No Rust test binary was added.
  • Real app: not applicable (no behaviour change, and nothing in libobs uses it yet).

Checklist

  • Base is master
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits (characterization test_deque landed in test/cmocka: Add util/deque.h characterization test #1548)
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): not applicable
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): none

The obs-output delay port (obs-rust#1546) has to push, peek and pop the C
struct deque that obs-output.c owns and frees, so obs-util needs a
Rust equivalent of the header-inline util/deque.h, as darray.h has.

This adds the safe core's API, obs_util::deque::Deque over a byte
buffer trait (a Vec, or later the bmalloc memory of a C deque), with
todo!() bodies, and Tier 1: every test_deque case but test_layout
ported 1:1 under the same names (test_layout becomes the layout test
with the struct mirror), and new edge cases for zero-size operations,
wraps at both ends, reserve without a wrap, place across the end and
the peek/pop size checks. The bnum_allocs() checks count through a
test buffer that counts like bmem, so they need no process-wide
counter.

All 27 fail with "not yet implemented".

Refs obs-rust#1546
The obs-output delay port (obs-rust#1546) keeps delay_data, a struct deque that
obs-output.c initializes and frees, and has to push, peek and pop it
from Rust with the same semantics as the C inlines. deque.h is
header-inline, so nothing swaps: like darray.h, the Rust equivalent is
a library the ports call, and the struct layout is the contract.

- obs_util::deque: the safe core. Deque is generic over its byte buffer
  (DequeBuf), so the same code runs on a Vec (VecBuf) and on a C deque's
  bmalloc memory. It follows the C step for step: the growth rule (the
  first push allocates exactly its size, then the capacity doubles or
  becomes the new size), the wrap points, end_pos == capacity when full
  and unwrapped, the move of the wrapped tail on growth and reserve, and
  the reset to position 0 when the deque empties.
- obs_util::ffi::deque: the repr(C) struct deque mirror and the C
  signatures (deque_init ... deque_data) as plain Rust functions. Each
  runs the core in place on the C struct, with memory from the bmem port,
  so C's deque_free frees what Rust allocated and the reverse.
- Tier 2: the layout test against the oracle, which also mirrors
  test_layout. Tier 3: oracle wrappers of the C inlines
  (oracle/deque.c); a proptest over random operation sequences on twin
  deques (Rust, C and the safe core), comparing the fields, the live
  bytes at their offsets and every peek, pop and deque_data result; a
  proptest that hands one deque to Rust or C at random per step,
  including deque_free; the test_deque record FIFO the same way; and the
  bnum_allocs() deltas in an isolated process.

No intentional differences from C. rust/README.md and the
header-inline list in testing-policy.md gain deque.h.

Refs obs-rust#1546
@rdoupe
rdoupe requested a review from zackees as a code owner October 20, 2026 20:24
@zackees
zackees enabled auto-merge October 20, 2026 20:26
@zackees
zackees added this pull request to the merge queue Oct 20, 2026
Merged via the queue into obs-rust:master with commit 1d26efc Oct 20, 2026
23 checks passed
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 20, 2026
* libobs: Add Tier 1 tests for the output delay port

RED for the port of libobs/obs-output-delay.c.h, the stream delay, to a
new crate, obs-output. The safe core (obs_output::delay) is generic over
the output: its delay fields, the queue, the delay lock and the libobs
calls the C file makes (DelayOutput), so the C ABI shim can run it on the
real struct obs_output through the hidden accessors and take delay_mutex
exactly where the C takes it. The pop decision is a pure function of the
front entry's time, the clock, the running delay and the preserve and
reconnecting flags; the clock is a parameter, read once per call. Every
function is a todo!() stub.

The first twenty cases mirror test_output_delay 1:1 (same names, same
assertions, in order) on a model output that records the libobs calls
and whether the lock was held, as the C test's stubs do. The rest pin the
edges: the strict comparison and the wrapping age, when preserve extends
the delay (also shorter than it was), the truncating seconds cast, one
lock per push and per pass and none in cleanup, an empty queue, a start
or stop at the front while preserving, a failed capture that releases
packets queued before it, and an entry the file never queues.

They are a module of obs-tests-core, so no new test binary
(ci/test_binary_cap.py: 8 of 8).

soldr cargo test -p obs-data --test obs-tests-core output:: fails 35 of
35 with "not yet implemented".

Refs obs-rust#1546

* libobs: Port the output stream delay to obs-output

libobs/obs-output-delay.c.h moves to obs_output::delay, a new crate named
after the component, so later units of obs-output.c can join it. Its
seven functions are what obs-output.c calls to hold an output's packets,
start and stop for the stream delay: obs_output_set_delay,
obs_output_get_delay and obs_output_get_active_delay (EXPORT), and
process_delay, obs_output_delay_start, obs_output_delay_stop and
obs_output_cleanup_delay (hidden; obs-output.c installs process_delay as
the encoded packet callback by name).

The safe core runs on any DelayOutput (the C output through the shim, or
a model in the tests) and takes the delay lock exactly where the C takes
delay_mutex: once per push and once per pass over the front, never in
cleanup, and never around a callback. The pop decision is a pure
function of the front entry's time, the clock, the running delay and the
preserve and reconnecting flags, with the strict comparison and the
wrapping age of the C. The six behaviours test_output_delay marks
"characterized, not endorsed" are kept.

The shim never reads struct obs_output: it goes through the hidden
obs_output_delay_fields() and obs_output_delay_signal() (obs-rust#1550), and
reads and writes each field at the point the C does, the os_atomic ones
as atomics of their C size. delay_data stays the C struct deque of
struct delay_data that obs-output.c frees; it is pushed, peeked and
popped in place with obs_util::ffi::deque (obs-rust#1561). Held packets are
libobs's reference-counted copies (obs_encoder_packet_create_instance,
hidden in libobs) and go back through obs_encoder_packet_release. Each
entry point reads os_gettime_ns() once and calls an *_at function with
it, which the parity test drives with a test clock.

The file leaves libobs.core-output for its own group,
libobs.ported-output-delay, whose PCH is obs-internal.h alone, so the
oracle and test_output_delay compile it without the core. The four
hidden symbols go in rust-hidden.txt, rust-exports.map and
rust-unexports-macos.txt; the three exported ones in
rust-exports.d/obs-output-output_delay.txt. The exported symbol sets are
unchanged.

Tier 3 (oracle/output_delay.c) compares the layouts of encoder_packet,
encoder_packet_time, delay_data and obs_output_delay_fields, the values
the file uses, the characterization scenarios, and random sequences of
packets, starts, stops, cleanups, setter and getter calls (null outputs
too), flag, result and clock changes: after every step the return value,
the recorded calls and whether the lock was held, the log line, the
packet copies, every delay field, the deque state and every queued
entry. Both sides go through the same accessor and libobs stand-ins. No
intentional differences. test_output_delay compiles the C into itself
and is unchanged.

Fixes obs-rust#1546
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.

2 participants