Skip to content

libobs: Port the context names to obs-core - #1813

Merged
zackees merged 4 commits into
obs-rust:masterfrom
rdoupe:port/context-names-obs-core
Oct 22, 2026
Merged

zackees merged 4 commits into
obs-rust:masterfrom
rdoupe:port/context-names-obs-core

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1787

What and why

PR 3 of the plan in #1787, the port: libobs/obs-context-names.c.h goes to rust/obs-core, module context_names. #1788 moved the two helpers out of obs.c.h behind hidden entry points and characterized them; #1804 fixed the rename warning and the NULL rename in obs.c.h. Every source, scene, canvas, encoder, output and service gets its name through obs_context_name_dup(), and the name tables make a taken name unique through obs_context_name_deduplicate(). The hybrid libobs drops the C file and links the Rust shims. obs.c.h (still C) keeps dup_name(), obs_context_deduplicate_name(), context_name_exists(), the name tables, the locks, the rename cache, the warnings and obs->data.unnamed_index.

  • Safe core (src/context_names.rs), on bytes, Option for NULL: unnamed_name() ("__unnamed%04lld": zero-padded to four characters, the sign included), suffixed_name() ("%s %d"), name_dup() and deduplicate(). deduplicate() asks its exists closure Lookup::Name (the name as given, NULL included), then each Lookup::Candidate from 2 up, once each, until one is free. That is the C's loop, question for question. Names are never parsed and never go through a formatter, so % sequences and bytes that are not UTF-8 are kept. The names come from scene collections, obs-websocket and scripts, so the module denies clippy's indexing_slicing, arithmetic_side_effects, unwrap_used, expect_used, panic, todo, unimplemented and unreachable.
  • Shim (src/ffi/context_names.rs): the two functions with the C signatures. The counter crosses as the caller's long long * and is read and counted up only for an unnamed name. exists is the caller's C callback (context_name_exists(), HASH_FIND_STR). The shim calls it as the C does: first with the caller's own name pointer, then with each candidate as a NUL-terminated buffer that is valid for that call (as the C's dstr is). The names are bmalloc'd and the caller bfrees them, as with the C's bstrdup and dstr_printf strings.
  • C side: the file leaves libobs.core for its own group, libobs.ported-context-names (LIBOBS_PORTED_SOURCES), whose PCH is obs-internal.h, so libobs-c-reference and the C backend keep compiling it. The functions are declared in obs-internal.h without EXPORT, so the shims are hidden: rust-hidden.txt, rust-exports.map (local:) and rust-unexports-macos.txt. libobs-rust already re-exports obs-core's ffi.
  • Kept, characterized, not endorsed (libobs: Move the context name helpers into obs-context-names.c #1788), unchanged: renaming "A 2" to "A" while "A" is taken leaves "A 2"; a taken "" would become " 2"; the counter is printed signed. In obs.c.h, unchanged: a rename to "" takes the next unnamed name without making it unique, and unnamed_index is incremented without a lock.
  • Edges, the same as C: a NULL name that exists() calls taken gets "(null) 2", which is what the C's "%s" prints for NULL (obs.c never asks for NULL since libobs: Name the requested name in the rename warning, and take a NULL rename #1804). The counter wraps at LLONG_MAX, as the C oracle does (signed overflow in C; libobs counts from 0).
  • Intentional differences, for pointers obs.c never passes: a null exists returns NULL without a call, and a null unnamed_index returns NULL where an unnamed name is needed. C calls through the one and dereferences the other. They are in the parity test header and rust/README.md.
  • Tier 2: test_context_name_helpers stays the C characterization. The issue plan's test_context_name_helpers_rust, the same file linked against libobs_rust-static, does not link on Linux without changes outside the test. The shims share their archive member with the rest of obs-core, which pulls in obs-media-io and obs-util, and they need libuuid, GIO and libobs's hidden base_default_log_handler. This is the same reason libobs: Port the hotkey press state machine to obs-hotkey #1786 and libobs: Port the portal shortcut combination sets to obs-hotkey #1800 dropped their variants. I dropped it and say so in the test's comments. Tier 3 compares the swap instead.
  • Docs: naming.md (obs-core row) and rust/README.md (the layout row, and the row for obs-context-names.c.h in the ported table).

Tier 1 (tests/obs_core/context_names.rs, a module of obs-tests-core), 19 tests:

  • test_context_name_helpers ported 1:1: its 12 cases under the same names, in the same order, with the same inputs and expected values, and the same scripted exists() with a log of every question. dedup_many takes 200 numbers where the C takes 100000; what it pins is the order of the questions.
  • Edge cases: the four-digit width with the sign; both ends of long long and the wrap at LLONG_MAX; the candidate format for any int suffix; a NULL name that is taken; the kind of each question; and two proptests. The first checks the dedup against the written-out rule, with the full question log, over any bytes and taken sets. The second checks the dup over any name, private flag and counter.

Tier 3 (tests/obs_core/context_names_parity.rs): the oracle, rust/obs-c-oracle/oracle/context_names.c, is the C file unmodified, with its two functions renamed and dstr_printf taken from the C-only oracle copy. Both implementations are called through the C signatures with the same scripted exists(): a set of taken names, or "taken for the first n questions", and whether NULL is taken. Compared: the result (NULL or the bytes), the counter, and the full exists() log. The log covers the count, the order, each name byte for byte with NULL included, and that the first call, and only the first, gets the caller's own pointer.

  • Exhaustive: 36 names and NULL. The names are empty, spaces, ASCII, % sequences, bytes that are not UTF-8, precomposed and decomposed é, names ending in digits or " N", "(null)", unnamed names and a 64 KiB name. Each runs against 29 taken sets (the name, runs of candidates, gaps, " 0" and " 1", candidates without the name, nested suffixes, the first n taken for n up to 12), with NULL taken or not.
  • The dup over those names and NULL, private on and off, at 122 counter values: near 0, near every power of ten with either sign (the widths of %04lld), the int32 and uint32 ends, and both ends of long long.
  • The counter through a run of 24 calls from four starting values.
  • Proptests, 4096 cases each: random names and taken sets, and any name, private flag and counter.
  • The two intentional differences are excluded and tested on the shims alone.

RED → GREEN

  • RED commit: 0ce8ea23b libobs: Add Tier 1 tests for the context names port. The safe core is todo!(), and cargo test -p obs-data --test obs-tests-core obs_core::context_names fails 19 of 19 with "not yet implemented".
  • GREEN: 5ab77b35f libobs: Port the context names to obs-core. Tier 1 passes 19/19. test_context_names (15/15, private Xvfb) and test_context_name_helpers (12/12) pass against libobs with the C backend, the hybrid libobs and libobs-c-reference. Tier 3, 1cab5ccd0 obs-c-oracle: Compare the context names port with the original C, passes 8/8. All of obs_core:: passes 56/56.
  • Deliberate breaks of the port (not committed). Each mutant is caught by Tier 3; all but the last are also caught by Tier 1. The columns count failing tests:
Mutant Tier 1 (of 19) Tier 3 (of 8)
suffix from 3 9 3
"%s%d" (no space) 10 3
an empty name copied, not unnamed 2 4
pre-increment of the counter 4 3
a private NULL name unnamed 2 3
one try only 7 3
%03lld 3 3
the counter not counted up 4 4
the name asked twice 9 3
one question past the free one 7 3
NULL printed as "" 2 2
the first question with a copy of the name, not the caller's pointer 0 2
  • Local, two build trees: OBS_LIBOBS_BACKEND=C, and HYBRID with OBS_BUILD_C_REFERENCE=ON, both full builds. Full ctest -j4, headless (private Xvfb, private D-Bus, fresh HOME):
Lane Library ctest Exported symbols
OBS_LIBOBS_BACKEND=C libobs focused: both context tests IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON hybrid libobs 373/373 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON libobs-c-reference 373/373 IDENTICAL (1766)

Nothing was skipped or failed to run. In the hybrid libobs, obs_context_name_dup and obs_context_name_deduplicate are local (t) Rust symbols, and the group's object is built only into libobs-c-reference.

  • Also run: uv run ci/local_gate.py --lane lint and --lane cross passed, and so did ci/test_build_groups.py and ci/test_ported_sources.py.
  • Real app (UI, protocol output or logging changes): not applicable (no behaviour change; test_context_names drives the swap through the real libobs's public API).

Windows/macOS: not built there. The port has no platform conditional. The shims are hidden on every host (rust-unexports-macos.txt lists them), and ci/test_ported_sources.py checks that the group is ported on Linux, Windows and macOS.

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • 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 (N/A for docs-only; a focused CI fix may push RED and GREEN together) (the characterization is libobs: Move the context name helpers into obs-context-names.c #1788)
  • 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): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

RED for the port of libobs/obs-context-names.c.h to rust/obs-core,
module context_names. The safe core is a stub (todo!()): unnamed_name()
("__unnamed%04lld"), suffixed_name() ("%s %d"), name_dup()
(obs_context_name_dup) and deduplicate() (obs_context_name_deduplicate).
Names are bytes, Option for NULL. deduplicate() asks its exists closure
one Lookup at a time: Lookup::Name, the name as given (NULL included),
then each Lookup::Candidate, as the C asks exists().

tests/obs_core/context_names.rs ports test_context_name_helpers 1:1: its
12 cases under the same names, in the same order, with the same inputs
and expected values and the same scripted exists() with a log of every
question; dedup_many takes 200 numbers where the C takes 100000. The
"characterized, not endorsed" cases of obs-rust#1788 stay pinned.

Edge cases: the four-digit width of the counter with the sign, both ends
of long long and the counter wrapping at LLONG_MAX, the candidate format
with any int suffix, a NULL name that exists() calls taken ("(null) 2",
as "%s" prints NULL), the kind of each question, and two proptests: the
dedup against the written-out rule over any bytes and taken sets, with
the full question log, and the dup over any name, private flag and
counter.

The tests are a module of the existing core category binary,
obs-tests-core.

soldr cargo test -p obs-data --test obs-tests-core obs_core::context_names
fails 19 of 19 with "not yet implemented".

Refs obs-rust#1787
Ports libobs/obs-context-names.c.h to the context_names module of
rust/obs-core. Every source, scene, canvas, encoder, output and service
gets its name through obs_context_name_dup(), and the name tables of
obs.c make a taken name unique through obs_context_name_deduplicate().
dup_name(), obs_context_deduplicate_name() and context_name_exists() in
obs.c.h (still C) call them and keep the name tables, the locks, the
rename cache, the warnings and obs->data.unnamed_index. The hybrid
libobs drops the C file and links the Rust shims.

Safe core (src/context_names.rs), on bytes, Option for NULL:
unnamed_name() ("__unnamed%04lld": zero-padded to four characters, the
sign included), suffixed_name() ("%s %d"), name_dup() and deduplicate().
deduplicate() asks its exists closure Lookup::Name, the name as given,
then each Lookup::Candidate from 2 up, once each, until one is free: the
C's loop, question for question. Names are never parsed and never go
through a formatter, so '%' sequences and bytes that are not UTF-8 are
kept. A NULL name that exists() calls taken gets "(null) 2", as the C's
"%s" prints NULL. The counter wraps at LLONG_MAX, as the C oracle does
(signed overflow in C; libobs counts from 0). The names come from scene
collections, obs-websocket and scripts, so the module denies clippy's
indexing_slicing, arithmetic_side_effects, unwrap_used, expect_used,
panic, todo, unimplemented and unreachable.

Kept as the C has it, characterized and not endorsed (obs-rust#1788): "A 2"
renamed to "A" while "A" is taken stays "A 2", a taken "" would become
" 2", and the counter is printed signed. obs.c.h keeps the rest: a
rename to "" takes the next unnamed name without the dedup, and the
counter is incremented without a lock.

Shim (src/ffi/context_names.rs): the two functions with the C
signatures. The counter crosses as the caller's long long *, read and
counted up only for an unnamed name. exists() is the caller's C
callback (context_name_exists(), HASH_FIND_STR), called as the C calls
it: first with the caller's own name pointer (NULL included), then with
each candidate as a NUL-terminated buffer valid for that call. The
names are bmalloc'd, as the C's bstrdup and dstr_printf strings, and
the caller bfrees them. Intentional differences, for pointers obs.c
never passes: a null exists returns NULL without a call, and a null
unnamed_index returns NULL where an unnamed name is needed (C calls
through and dereferences them).

C side: the file leaves libobs.core for its own group,
libobs.ported-context-names (LIBOBS_PORTED_SOURCES), whose PCH is
obs-internal.h, so the C reference and the C backend keep compiling it.
The functions are declared in obs-internal.h without EXPORT, so the
shims are hidden: rust-hidden.txt, rust-exports.map and
rust-unexports-macos.txt. libobs-rust already re-exports obs-core's
ffi.

Results: test_context_names (15 of 15, under a private Xvfb) and
test_context_name_helpers (12 of 12) pass against the C-backend libobs,
the hybrid libobs and libobs-c-reference. The exported symbol sets are
identical, 1766 in each. Tier 1: obs_core::context_names 19 of 19.

Refs obs-rust#1787
The oracle, oracle/context_names.c, is libobs/obs-context-names.c.h,
unmodified, with its two functions renamed to oracle_* and dstr_printf
to the C-only oracle_dstr_printf. bmalloc, brealloc, bfree and bmemdup
keep their real names (obs-util's under cargo test), so the oracle's
names are bfree'd like the shims'.

Tier 3 (tests/obs_core/context_names_parity.rs), in obs-tests-core,
calls both through the C signatures with the same scripted exists(): a
set of taken names, or "taken for the first n questions", and whether
NULL is taken, with a log of every call. Compared: the result (NULL or
the bytes), the full exists() log (count, order, each name byte for
byte, NULL included), that the first call, and only the first, gets the
caller's own name pointer, and the counter after the dup.

- exhaustively, 36 names and NULL (empty, spaces, ASCII, '%' sequences,
  bytes that are not UTF-8, decomposed and precomposed e-acute, names
  ending in digits or " N", "(null)", unnamed names, a 64 KiB name), each
  against 29 taken sets (the name, runs of candidates, gaps, " 0" and
  " 1", candidates without the name, nested suffixes, "the first n taken"
  for n up to 12), with NULL taken or not;
- the dup over those names and NULL, private on and off, at 122 counter
  values: near 0 and near every power of ten with either sign (the
  widths of %04lld), the int32 and uint32 ends, and both ends of long
  long, LLONG_MAX included (it wraps in both);
- the counter through a run of 24 calls from four starting values;
- proptests (4096 cases each) over random names and taken sets, and over
  any name, private flag and counter.

Excluded, the intentional differences, tested on the shims alone: a null
exists returns NULL without a call, and a null unnamed_index returns
NULL where an unnamed name is needed (C calls through and dereferences
them). 8 of 8 pass.

Deliberate breaks of the port, not committed, each caught by Tier 3
(and Tier 1, failures out of 19, except the last):

| Mutant                                 | Tier 1 | Tier 3 |
| suffix from 3                          | 9      | 3 of 8 |
| "%s%d" (no space)                      | 10     | 3      |
| an empty name copied, not unnamed      | 2      | 4      |
| pre-increment of the counter           | 4      | 3      |
| a private NULL name unnamed            | 2      | 3      |
| one try only                           | 7      | 3      |
| %03lld                                 | 3      | 3      |
| the counter not counted up             | 4      | 4      |
| the name asked twice                   | 9      | 3      |
| one question past the free one         | 7      | 3      |
| NULL printed as ""                     | 2      | 2      |
| the first question with a copy of name | 0      | 2      |

test_context_name_helpers stays the C characterization. A _rust
variant against libobs_rust-static does not link on its own: the
shims' archive member holds the rest of obs-core, which pulls in
obs-media-io and obs-util (libuuid, GIO, and libobs's hidden
base_default_log_handler). Its comments say so.

Refs obs-rust#1787
naming.md: obs-context-names.c.h joins the units of libobs/obs.c.h that
go in rust/obs-core. rust/README.md: the obs-core layout row names it,
and the obs-context-names.c.h row of the ported table: the swap, what
stays in obs.c.h, how exists() is called, the behaviour kept from obs-rust#1788,
and the two intentional differences (a null exists and a null
unnamed_index, which obs.c never passes). obs-core's tests are already
in the core category binary, obs-tests-core.

Fixes obs-rust#1787
@rdoupe
rdoupe requested a review from zackees as a code owner October 22, 2026 18:47
@zackees
zackees added this pull request to the merge queue Oct 22, 2026
Merged via the queue into obs-rust:master with commit c48234a Oct 22, 2026
32 checks passed
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.

rust: Port the context name dedup and unnamed names in obs.c to obs-core

2 participants