Skip to content

Generating C++ to Rust binding so that the Wayland C++ objects can publish events through Rust to the clients, but not yet fully connecting them#4757

Merged
Saviq merged 41 commits intomainfrom
feature/wayland-rs-event-gen
Mar 23, 2026
Merged

Generating C++ to Rust binding so that the Wayland C++ objects can publish events through Rust to the clients, but not yet fully connecting them#4757
Saviq merged 41 commits intomainfrom
feature/wayland-rs-event-gen

Conversation

@mattkae
Copy link
Copy Markdown
Contributor

@mattkae mattkae commented Mar 9, 2026

What's new?

  • Generating src/middleware.rs, which contains Rust structs that ferry data from Rust to C++
  • Generating src/ffi.rs, which contains:
    1. FFI definitions for the middleware structs that sanitize the data going across the language boundary (C++ -> Rust)
    2. FFI definitions for the C++ implementation classes (Rust -> C++)
  • Adding an associate method to each Wayland interface's C++ class. This class will "associate" the Box<InterfaceExt> struct with the C++ class so that the C++ class can call methods on its underlying Rust implementation
  • Introduced include/ffi_fwd.h to get around a circular dependency issue where the generated C++ headers want to include the Rust headers, but the Rust headers want to include the generated C++ headers. This came about because the <Interface>Impl class now hold an <Interface>Ext box.

How to test

  1. Run cargo build from src/wayland/wayland_rs
  2. See src/middleware.rs
  3. See src/ffi.rs
  4. See src/dispatch.rs
image image

Checklist

  • Tests added and pass
  • Adequate documentation added
  • (optional) Added Screenshots or videos

@mattkae mattkae changed the title Generating C++ to Rust binding so that the Wayland C++ objects can publish events through Rust to the clients Generating C++ to Rust binding so that the Wayland C++ objects can publish events through Rust to the clients, but not yet fully connecting them Mar 11, 2026
@mattkae mattkae requested review from RAOF and robert-ancell March 11, 2026 20:16
@mattkae mattkae marked this pull request as ready for review March 11, 2026 20:27
@mattkae mattkae requested a review from a team as a code owner March 11, 2026 20:27
Copilot AI review requested due to automatic review settings March 11, 2026 20:27
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR restructures the wayland_rs Rust/C++ CXX bridge so generated Wayland interface C++ classes can be associated with Rust “extension” structs, enabling a future path for emitting Wayland events from C++ via Rust to clients.

Changes:

  • Switch the CXX bridge entrypoint from src/lib.rs to generated src/ffi.rs and update the demo to include ffi.rs.h.
  • Add build-script generation for middleware.rs (Rust extension structs) and ffi.rs (CXX bridge definitions), plus an associate() method on each generated C++ interface.
  • Refactor protocol parsing and code generation helpers; add ffi_fwd.h generation to avoid circular includes.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/wayland/wayland_rs/src/lib.rs Replaces inline module declarations with include!() of generated dispatch.rs, protocols.rs, middleware.rs, and ffi.rs.
src/wayland/wayland_rs/example/main.cpp Updates the demo to include the new generated CXX header ffi.rs.h.
src/wayland/wayland_rs/build_script/protocol_parser.rs Sorts protocol XML inputs for deterministic generation and simplifies iteration.
src/wayland/wayland_rs/build_script/protocol_middleware_generation.rs Adds generator for Rust “Ext” wrappers that forward event calls to wayland_server protocol objects.
src/wayland/wayland_rs/build_script/main.rs Major generation pipeline rework: middleware + ffi generation, ffi_fwd.h, new dispatch storage model (Arc<Mutex<...>>), and CXX bridge moved to src/ffi.rs.
src/wayland/wayland_rs/build_script/helpers.rs Extracts shared helpers for writing generated Rust/C++ output and naming helpers.
src/wayland/wayland_rs/build_script/ffi_generation.rs Generates the unified #[cxx::bridge] mod ffi with Rust->C++ and C++->Rust declarations.
src/wayland/wayland_rs/build_script/cpp_builder.rs Updates C++ signature generation (return-by-value for unique_ptr, introduces rust::Box<T>, sanitizes identifiers) and pins C++ method receivers in Rust bindings.
src/wayland/wayland_rs/CMakeLists.txt Points CMake at src/ffi.rs as the bridge source and adjusts include directories for the demo target.
src/wayland/wayland_rs/.gitignore Updates ignored generated Rust files (ffi.rs, middleware.rs) and removes old ffi_cpp.rs.

Comment thread src/wayland/wayland_rs/build_script/main.rs Outdated
Comment thread src/wayland/wayland_rs/build_script/main.rs Outdated
Comment thread src/wayland/wayland_rs/build_script/main.rs
Comment thread src/wayland/wayland_rs/build_script/main.rs Outdated
Comment thread src/wayland/wayland_rs/build_script/main.rs
Copy link
Copy Markdown
Contributor

@robert-ancell robert-ancell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've read through this; there's a lot going on and I can only follow parts of it. I can't see any issues but it needs a second opinion / someone more knowledgeable in Rust to have a look too.

Copy link
Copy Markdown
Contributor

@RAOF RAOF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual code generation code is fine.

I'm still digesting the code that it's generating; it seems at this point that we probably need more documentation about the high-level theory of what's happening here, because it's very much non-obvious.

Comment on lines +30 to +36
pub fn dash_to_snake(name: &str) -> String {
name.replace('-', "_")
}

pub fn dash_to_snake_ident(name: &str) -> Ident {
format_ident!("{}", dash_to_snake(name))
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are unused, right?

Incidentally, if there's much more case manipulation needed, heck might be a good choice.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dash_to_snake_ident is still used

@mattkae
Copy link
Copy Markdown
Contributor Author

mattkae commented Mar 19, 2026

The actual code generation code is fine.

I'm still digesting the code that it's generating; it seems at this point that we probably need more documentation about the high-level theory of what's happening here, because it's very much non-obvious.

I was going to leave this as a task for later where I write a big diagram/markdown of what is getting generated and why. I am waiting for "later" because things feel prone to change now, and I don't want to keep rewriting it

@mattkae mattkae requested a review from RAOF March 19, 2026 12:09
@mattkae
Copy link
Copy Markdown
Contributor Author

mattkae commented Mar 19, 2026

@RAOF I update to use pin_mut, UniquePtr, and with comments to make things easier to follow :)

@mattkae mattkae requested a review from robert-ancell March 19, 2026 21:08
@github-actions
Copy link
Copy Markdown

TICS Quality Gate

✔️ Passed

mir

Coding Standards: ✔️ Passed

✔️ Condition “No new Coding Standard Violations for level 1, 2, 3 with respect to Previous analysis” passed.

See the results in the TICS Viewer

The following files have been checked for this project
  • src/wayland/wayland_rs/build_script/cpp_builder.rs
  • src/wayland/wayland_rs/build_script/ffi_generation.rs
  • src/wayland/wayland_rs/build_script/helpers.rs
  • src/wayland/wayland_rs/build_script/main.rs
  • src/wayland/wayland_rs/build_script/protocol_middleware_generation.rs
  • src/wayland/wayland_rs/build_script/protocol_parser.rs
  • src/wayland/wayland_rs/example/main.cpp
  • src/wayland/wayland_rs/src/lib.rs
  • src/wayland/wayland_rs/src/wayland_server.rs

TICS / TICS / Run TICS analysis

Copy link
Copy Markdown
Contributor

@RAOF RAOF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need GitHub to make it more clear when I haven't actually submitted the review!

This is somewhat weird code, but the weirdness is now documented, and once we've got the whole thing implemented we can see if anything can be simplified.

@RAOF RAOF added this pull request to the merge queue Mar 22, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 22, 2026
@Saviq Saviq added this pull request to the merge queue Mar 23, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 23, 2026
@Saviq Saviq added this pull request to the merge queue Mar 23, 2026
Merged via the queue into main with commit 689dfe2 Mar 23, 2026
36 of 38 checks passed
@Saviq Saviq deleted the feature/wayland-rs-event-gen branch March 23, 2026 14:51
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.

5 participants