Skip to content

enum CRef: separate from struct CBox and simplify but expand the API#1471

Merged
kkysen merged 7 commits intomainfrom
kkysen/enum-CRef
Feb 13, 2026
Merged

enum CRef: separate from struct CBox and simplify but expand the API#1471
kkysen merged 7 commits intomainfrom
kkysen/enum-CRef

Conversation

@kkysen
Copy link
Collaborator

@kkysen kkysen commented Feb 12, 2026

This does some API simplification for #1439 while also supporting other impl AsRef types. It separates CRef from CBox. CBox is the pure C abstraction over an owned C reference/"Box". CRef is an enum over CBox and other Rust-native impl AsRefs. I tried using dyn AsRef first, but we make some safety guarantees about reference stability that we can't make about any impl AsRef, and that would force an extra Box. Moving the dyn AsRef one level higher to Arc<dyn AsRef<T>> would be super nice, but in order to send the Arcs through FFI boundaries, they can't wrap an unsized type, as we can't send unsized pointers over FFI boundaries (the ptr metadata API is still very unstable).

Adding support for non 'static &Ts would be extremely useful and would allow us to eliminate a data copy, but that is much more tricky, as we'd have to thread the lifetime through a lot of places and probably rearrange some of the Rav1dContext/Rav1dState API.

This allows other types, like `Vec<u8>`, `[u8; N]`, `&[u8]`, `Arc<[u8]>`
instead of just `Box<[u8]>` (assuming `T = [u8]`).
However, it requires an extra `Box` allocation and indirection.
But the allocation is small and not very frequent, so it should be fine,
and `CArc` stores a direct ptr, avoiding the extra indirection penalty
(`CBox` is generally always accessed through `CArc` in `rav1d`).
…directly

This simplifies a bunch of the APIs.
This avoids the extra `Box` and lets us make safety guarantees
about each type's reference stability, which we depend on.
For example, `Vec` doesn't have stable references
(a wrapper than can't resize would, which we can add if needed).
@kkysen kkysen requested a review from randomPoison February 12, 2026 10:47
Base automatically changed from kkysen/fn-Rav1dData-wrap_box to main February 12, 2026 21:18
@kkysen kkysen merged commit 3b2655a into main Feb 13, 2026
28 checks passed
@kkysen kkysen deleted the kkysen/enum-CRef branch February 13, 2026 09:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants