Repository navigation
obs-util: Add a Rust equivalent of util/deque.h - #1561
Merged
Merged
Conversation
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
zackees
approved these changes
Oct 20, 2026
zackees
enabled auto-merge
October 20, 2026 20:26
6 of 7 tasks
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 Cstruct dequethatobs-output.cinitializes 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.his header-inline, so nothing swaps: likedarray.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.rust/obs-util/src/deque.rs):Deque<B: DequeBuf = VecBuf>holds the four size fields (DequeState) and a byte buffer.DequeBufisbrealloc/bfreeplus the bytes, so the same code runs on aVec(VecBuf) and on a C deque'sbmallocmemory.end_pos == capacitywhen the deque is full and unwrapped (characterized, not endorsed);reserve;upsizeispush_back_zeroof the difference, as in the C body. A null pop destination isdiscard_front/discard_back. Peek and pop past the size panic, where C asserts.cfg, nounsafe.rust/obs-util/src/ffi/deque.rs):#[repr(C)] struct dequemirror 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 plainunsafe fns.brealloc/bfree, throughffi/darray.rs), so C'sdeque_freefrees what Rust allocated, and Rust grows and frees what C allocated.#[no_mangle]), sorust-exports.d,rust-hidden.txtand the export maps are unchanged.deque_reorder_dataanddeque_ensure_capacityare internal steps of the other functions. They are not exposed on their own.rust/obs-c-oracle/oracle/deque.c,src/deque.rs): non-inlineoracle_deque_*wrappers of every inline function above, plussizeof,_Alignofand every field offset ofstruct deque. Likedarray.c, it includes the real header.rust/README.mdrow, andutil/deque.hadded to the header-inline list in testing-policy.md. naming.md needs no change:libobs/util/already maps toobs-util.test_deque(16 cases) is unchanged. The functions arestatic inline, so it tests the C header in both libobs builds and cannot see the Rust side. Tier 3 is the proof.RED → GREEN
73284231aobs-util: Add Tier 1 tests for the util/deque.h equivalent. The safe core's methods aretodo!().soldr cargo test -p obs-util --lib tests::containers::dequefails 27 of 27 with "not yet implemented"; the twoshould_paniccases fail on the wrong panic message.tests/containers/deque.rs) ports everytest_dequecase excepttest_layout1:1, under the same names withouttest_.test_layoutis mirrored by the layout test. Thebnum_allocs()checks count through a test buffer that counts like bmem, so they need no process-wide counter.placepast 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,reserveon an unwrapped offset and on an empty deque that has a buffer,placeacross the end inside the size, the two size checks, andVecBuf::free.b640fbc88obs-util: Add a Rust equivalent of util/deque.h.tests::containers::dequepasses 33/33: 27 Tier 1, 2 layout and 4 Tier 3.deque_layout.rs): size, alignment and every field offset against the oracle, plus thetest_layoutassertions (five pointer-sized fields in order, pointer alignment).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 aVec). The operations are every push, pop with and without a destination, peek,reserve,upsize,place,deque_data(in range, past the size, andSIZE_MAX) andfree. After every step it compares the four fields, whether the buffer is null, every live byte at its buffer offset, and each peek, pop anddeque_dataresult, 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 Cstruct dequethat each step hands to the Rust or the C functions at random,deque_freeincluded, against a C-only twin. This is the obs-output case.record_fifo_matches_c: thetest_dequerecord FIFO (127 pushes, 96 pops, growth to 1024 bytes) run the same mixed way.bmem_accounting_matches_c: thebnum_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, anddeque_freereleases it, whether C or Rust allocated it.max(size)tomax(size + 1): 19 of 27 Tier 1 tests and all 4 Tier 3 tests failed, and the proptests shrank to a singlePushBack.push_backwrap condition changed fromnew_end_pos > capacityto>=: all 4 Tier 3 tests failed.ENABLE_RUST_LIBOBS=OFFandON). Each runs the cmocka suite againstlibobs(OFF) andlibobs-with-rust(ON), headless (private Xvfb, private D-Bus, fresh HOME):ENABLE_RUST_LIBOBS=OFF)ENABLE_RUST_LIBOBS=ON)test_dequepasses 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 forx86_64-pc-windows-msvcandaarch64-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-runlink forx86_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.Checklist
mastertest_dequelanded in test/cmocka: Add util/deque.h characterization test #1548)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): not applicable