Repository navigation
libobs: Port the context names to obs-core - #1813
Merged
Merged
Conversation
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
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.
Fixes #1787
What and why
PR 3 of the plan in #1787, the port:
libobs/obs-context-names.c.hgoes torust/obs-core, modulecontext_names. #1788 moved the two helpers out ofobs.c.hbehind hidden entry points and characterized them; #1804 fixed the rename warning and the NULL rename inobs.c.h. Every source, scene, canvas, encoder, output and service gets its name throughobs_context_name_dup(), and the name tables make a taken name unique throughobs_context_name_deduplicate(). The hybrid libobs drops the C file and links the Rust shims.obs.c.h(still C) keepsdup_name(),obs_context_deduplicate_name(),context_name_exists(), the name tables, the locks, the rename cache, the warnings andobs->data.unnamed_index.src/context_names.rs), on bytes,Optionfor NULL:unnamed_name()("__unnamed%04lld": zero-padded to four characters, the sign included),suffixed_name()("%s %d"),name_dup()anddeduplicate().deduplicate()asks itsexistsclosureLookup::Name(the name as given, NULL included), then eachLookup::Candidatefrom 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'sindexing_slicing,arithmetic_side_effects,unwrap_used,expect_used,panic,todo,unimplementedandunreachable.src/ffi/context_names.rs): the two functions with the C signatures. The counter crosses as the caller'slong long *and is read and counted up only for an unnamed name.existsis the caller's C callback (context_name_exists(),HASH_FIND_STR). The shim calls it as the C does: first with the caller's ownnamepointer, then with each candidate as a NUL-terminated buffer that is valid for that call (as the C'sdstris). The names arebmalloc'd and the callerbfrees them, as with the C'sbstrdupanddstr_printfstrings.libobs.corefor its own group,libobs.ported-context-names(LIBOBS_PORTED_SOURCES), whose PCH isobs-internal.h, so libobs-c-reference and the C backend keep compiling it. The functions are declared inobs-internal.hwithoutEXPORT, so the shims are hidden:rust-hidden.txt,rust-exports.map(local:) andrust-unexports-macos.txt.libobs-rustalready re-exports obs-core'sffi."A 2"to"A"while"A"is taken leaves"A 2"; a taken""would become" 2"; the counter is printed signed. Inobs.c.h, unchanged: a rename to""takes the next unnamed name without making it unique, andunnamed_indexis incremented without a lock.exists()calls taken gets"(null) 2", which is what the C's"%s"prints for NULL (obs.cnever asks for NULL since libobs: Name the requested name in the rename warning, and take a NULL rename #1804). The counter wraps atLLONG_MAX, as the C oracle does (signed overflow in C; libobs counts from 0).obs.cnever passes: a nullexistsreturns NULL without a call, and a nullunnamed_indexreturns NULL where an unnamed name is needed. C calls through the one and dereferences the other. They are in the parity test header andrust/README.md.test_context_name_helpersstays the C characterization. The issue plan'stest_context_name_helpers_rust, the same file linked againstlibobs_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 hiddenbase_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.naming.md(obs-core row) andrust/README.md(the layout row, and the row forobs-context-names.c.hin the ported table).Tier 1 (
tests/obs_core/context_names.rs, a module ofobs-tests-core), 19 tests:test_context_name_helpersported 1:1: its 12 cases under the same names, in the same order, with the same inputs and expected values, and the same scriptedexists()with a log of every question.dedup_manytakes 200 numbers where the C takes 100000; what it pins is the order of the questions.long longand the wrap atLLONG_MAX; the candidate format for anyintsuffix; 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 anddstr_printftaken from the C-only oracle copy. Both implementations are called through the C signatures with the same scriptedexists(): 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 fullexists()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.%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.%04lld), theint32anduint32ends, and both ends oflong long.RED → GREEN
0ce8ea23blibobs: Add Tier 1 tests for the context names port. The safe core istodo!(), andcargo test -p obs-data --test obs-tests-core obs_core::context_namesfails 19 of 19 with "not yet implemented".5ab77b35flibobs: Port the context names to obs-core. Tier 1 passes 19/19.test_context_names(15/15, private Xvfb) andtest_context_name_helpers(12/12) pass against libobs with the C backend, the hybrid libobs and libobs-c-reference. Tier 3,1cab5ccd0obs-c-oracle: Compare the context names port with the original C, passes 8/8. All ofobs_core::passes 56/56."%s%d"(no space)%03lld""OBS_LIBOBS_BACKEND=C, andHYBRIDwithOBS_BUILD_C_REFERENCE=ON, both full builds. Fullctest -j4, headless (private Xvfb, private D-Bus, fresh HOME):OBS_LIBOBS_BACKEND=CHYBRID,OBS_BUILD_C_REFERENCE=ONHYBRID,OBS_BUILD_C_REFERENCE=ONNothing was skipped or failed to run. In the hybrid libobs,
obs_context_name_dupandobs_context_name_deduplicateare local (t) Rust symbols, and the group's object is built only into libobs-c-reference.uv run ci/local_gate.py --lane lintand--lane crosspassed, and so didci/test_build_groups.pyandci/test_ported_sources.py.test_context_namesdrives 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.txtlists them), andci/test_ported_sources.pychecks that the group is ported on Linux, Windows and macOS.Checklist
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): built with scripting ON (PR CI builds no OBS app: rungh workflow run ci.yml --ref <branch>for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)