Repository navigation
feat(obs-util): port util/dstr.c to Rust (#6) - #51
Closed
b7216309-jpg wants to merge 6 commits into
Closed
b7216309-jpg wants to merge 6 commits into
b7216309-jpg wants to merge 6 commits into
Conversation
Adds the C ABI shim for the ten EXPORT functions of libobs/util/lexer.c, the #[repr(C)] mirrors of strref, base_token, error_item, error_data and lexer, the oracle wrapper, the ENABLE_RUST_LIBOBS guard, rust-exports.txt entries, and Tier 1, Tier 2 layout and Tier 3 tests. The safe core bodies are todo!(), so soldr cargo test -p obs-util --test lexer fails 0/16 and --test lexer_parity aborts on the first call; the layout test passes. The lexer oracle needs dstr_catf and bmemdup. oracle/test_dstr.c is a test-only dstr_catf (dstr_vcatf with plain brealloc growth), like test_bmem.c for bmem, so the oracle does not pull in util/dstr.c and its utf8.c dependency. bmemdup joins test_bmem.c. Refs obs-rust#6 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Implements the safe core of libobs/util/lexer.c. GREEN for Tier 1 (16/16), the layout tests (5/5) and Tier 3 (9/9). Characters compare as the platform's C char (c_char), so bytes from 0x80 order as in C on both signed (x86) and unsigned (Arm Linux) char targets. toupper is the C locale's, ASCII only. Intentional difference: lexer_getstroffset leaves the outputs untouched when lex->text is NULL, where C dereferences it. Refs obs-rust#6 Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # libobs/cmake/rust-exports.txt # rust/obs-c-oracle/build.rs # rust/obs-c-oracle/src/lib.rs
Behavior-neutral move, as the function-extraction policy requires. These 15 functions move verbatim from util/dstr.c into the new util/dstr-libc.c: - the printf family (dstr_printf, dstr_catf, dstr_vprintf, dstr_vcatf), which takes C varargs and va_list; - the wide-character compares (wstrcmpi, wstrcmp_n, wstrcmpi_n, wstrstri, wcsdepad), which depend on towupper and wchar_t width; - the conversions (dstr_from_mbs, dstr_to_mbs, dstr_to_wcs, dstr_from_wcs, dstr_to_upper, dstr_to_lower), which go through platform.c and the locale. wstrblank moves with them, and the static is_padding helper, which both files use, is copied. No header, signature or behavior changes, and the new file is always compiled. What stays in dstr.c is pure byte-string logic that a Rust port can replace under ENABLE_RUST_LIBOBS. Stable Rust cannot define variadic functions, so the printf family has to stay in C. Refs obs-rust#6 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Adds the C ABI shim for the 26 EXPORT functions left in libobs/util/dstr.c after the dstr-libc.c split, the #[repr(C)] mirror of struct dstr, the oracle wrapper, the ENABLE_RUST_LIBOBS guard, rust-exports.txt entries, and Tier 1, layout and Tier 3 tests. The safe core bodies are todo!(), so soldr cargo test -p obs-util --test dstr fails 0/24 and --test dstr_parity aborts on the first call; the layout test passes. The core is generic over DstrStore, the dstr state plus the allocator calls C makes on it; the shim implements it on the caller's struct dstr with bmalloc/brealloc/bfree, so C and Rust keep sharing dstrs without a copy per call. The test_dstr.c stand-in stays: dstr_catf now lives in dstr-libc.c, which stays in C. Refs obs-rust#6 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Implements the safe core of the byte-string half of libobs/util/dstr.c. GREEN for Tier 1 (24/24), the layout test (1/1) and Tier 3 (5/5). Growth follows the dstr.h inline dstr_ensure_capacity, so capacity matches C after every call. Sources are read before the destination changes; where C reads past a source or through a NULL one, the port reads NUL. Intentional differences, where C is undefined, crashes or hangs: a source inside the destination reads as it was before the call; out-of-range insert/insert_dstr/insert_ch/remove/right arguments do nothing; a NULL or empty dstr_replace find does nothing (C dereferences NULL or loops forever); dstr_insert_ch does not write the extra byte C writes past a full buffer; strlist_split on NUL returns the whole string. Refs obs-rust#6 Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Oct 9, 2026
Contributor
Author
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 #43 (Phase 3 tracking; #6 closed when #33 merged)
Ports the byte-string half of
libobs/util/dstr.cto Rust: 26EXPORTfunctions. These areastrcmpi,astrcmp_n,astrcmpi_n,astrstri,strdepad,strlist_split/strlist_free, and thedstrcopy/ncopy/cat/ncat/insert/remove/replace/safe_printf/depad/left/mid/right family. The printf family, the wide-char compares and the conversions moved todstr-libc.cin #50 and stay in C. Stable Rust can't define variadic functions, and the rest depend on the locale or libc.Commits (new in this PR)
struct dstrmirror, the oracle, the guard,rust-exports.txtand the tests, against atodo!()core. Tier 1 fails 0/24, Tier 3 aborts, and layout passes.Design
dstrfunctions are generic over aDstrStoretrait: the struct's state (array/len/capacity) plus the allocator calls C makes (brealloc,bmallocforbmemdup,bfree). The shim implements it directly on the caller'sstruct dstr, so a loop ofdstr_catstays linear.VecDstrimplements it for Rust callers and tests.dstr.hinlinedstr_ensure_capacitygrowth rule (exact first allocation, then doubling). The inline helpers themselves stay in C under the header-inline exemption, and the layout test coversstruct dstr.dstr_cat_dstr(d, d)is well-defined in C and gives the same result. Where C reads moved or freed memory (dstr_ncopy(d, d->array + 1, n)frees before copying, anddstr_insert_dstr(d, i, d)memcpys from a moved buffer), Rust reads the source as it was. This also keeps the shim free of&/&mutaliasing.Intentional differences (C is undefined, crashes or hangs)
dstr_ncopy_dstrfrom an empty dstr,dstr_mid/dstr_leftpast the end).insert/insert_dstr/insert_chwithidx > len,removewithidx + count > len, andrightwithpos > lendo nothing (C wrapssize_tand moves memory).find:dstr_replacewith a NULLfinddoes nothing (C dereferences it); an emptyfinddoes nothing too (C loops forever, astest_dstrnotes).dstr_insert_chmid-string does not write C's extra byte past the buffer when capacity is exactlylen + 2(the out-of-bounds writetest_dstrworks around).strlist_splitwithsplit_ch == '\0'returns the whole string (C reads past the terminator).Characterized and kept:
dstr_removewithcount == lenfrees whateveridxis;dstr_replacerestarts the search after each replacement;dstr_ncatcopies embedded NULs; compares use the platform'scharsignedness.Tier 3: model-based parity
dstr_ops_match_cruns a random sequence of up to 24 operations (all 20 kinds, including self-sources,dstr_leftin place, re-initialization, and NUL-bearingncatbytes) on a Rust-shimmed dstr and an oracle dstr. After every step it checks that both agree onarraybeing NULL, the C string,lenandcapacity. Arguments stay inside C's defined behavior, so the oracle is never asked to do something undefined. Separate properties cover the compares andastrstri,strdepad(in place, returning its argument) andstrlist_split.Mutation check. I ran 23 mutations. Every one that can change observable behavior is caught: by parity, or, for
dstr_ncat's NUL-first check (which the shim already enforces before the core is reached), by Tier 1.ncopycapacity +1lentaking the middle pathinsert_chreservinglen + 1removehonoringidxon a full removereplacenot restarting after a shrinkreplacegrowth not doublingsafe_printfskipping$4depadkeeping an empty stringrightover-reservingastrcmpiastrcmp_ncomparing n+1splitdropping emptiesmidshort by onencopy_dstrnot freeing firstncat_dstrnot clampingncatdropping embedded NULscopyfreeing only a non-NULL array (the NULL-with-capacity state is unreachable);removetail via memmove (it writes the same NUL);leftignoringsame(the shim passes an empty source then);strdepadstopping at index 1 (buf[0]is never padding there).The first generator version missed the
ncatNUL-first case; theraw()generator was added for it.Verification
On Windows 11 (MSVC):
soldr cargo test --workspace --lockedpasses (132 tests)../lintpasses.-D warningsis clean for x86_64/aarch64 Linux and aarch64 macOS.Not verified locally
test_dstrruns unchanged against the Rust symbols with ON, and every C user ofdstrin libobs links against them.There is no Docker or WSL on this machine.
🤖 Generated with Claude Code