Add partial support for safe shared structs via the #[safe_shared_extern] attribute - #1715
Open
kolayne wants to merge 3 commits into
Open
Add partial support for safe shared structs via the #[safe_shared_extern] attribute#1715kolayne wants to merge 3 commits into
#[safe_shared_extern] attribute#1715kolayne wants to merge 3 commits into
Conversation
kolayne
force-pushed
the
safe-shared-extern
branch
6 times, most recently
from
May 25, 2026 15:49
3c9eb41 to
cdfe829
Compare
kolayne
force-pushed
the
safe-shared-extern
branch
2 times, most recently
from
May 26, 2026 02:30
a24c8fb to
30a6c11
Compare
kolayne
added a commit
to kolayne/xgrammar-rs
that referenced
this pull request
May 28, 2026
Rewrites the library to rely on `cxx` rather than `autocxx` as the bindings back-end. Updates the building script accordingly. Note that a custom version of cxx is used: it includes the patch dtolnay/cxx#1715 for the `#[safe_shared_extern]` attribute. Motivation for the change: wasm32 support (trymirai#8). xgrammar-rs now compiles for `wasm32-wasip1` (and, supposedly, other wasi targets), though for now it still requires the `WASI_SYSROOT` environment variable to be set to the path to [wasi-sdk](https://github.com/WebAssembly/wasi-sdk) version 33 or newer compiled with exception support (`-DWASI_SDK_EXCEPTIONS=ON`).
kolayne
added a commit
to kolayne/xgrammar-rs
that referenced
this pull request
May 28, 2026
Rewrites the library to rely on `cxx` rather than `autocxx` as the bindings back-end. Updates the building script accordingly. Note that a custom version of cxx is used: it includes the patch dtolnay/cxx#1715 for the `#[safe_shared_extern]` attribute. Motivation for the change: wasm32 support (trymirai#8). xgrammar-rs now compiles for `wasm32-wasip1` (and, supposedly, other wasi targets), though for now it still requires the `WASI_SYSROOT` environment variable to be set to the path to [wasi-sdk](https://github.com/WebAssembly/wasi-sdk) version 33 or newer compiled with exception support (`-DWASI_SDK_EXCEPTIONS=ON`).
kolayne
added a commit
to kolayne/xgrammar-rs
that referenced
this pull request
May 28, 2026
Rewrites the library to rely on `cxx` rather than `autocxx` as the bindings back-end. Updates the building script accordingly. Note that a custom version of cxx is used: it includes the patch dtolnay/cxx#1715 for the `#[safe_shared_extern]` attribute. Motivation for the change: wasm32 support (trymirai#8). xgrammar-rs now compiles for `wasm32-wasip1` (and, supposedly, other wasi targets), though for now it still requires the `WASI_SYSROOT` environment variable to be set to the path to [wasi-sdk](https://github.com/WebAssembly/wasi-sdk) version 33 or newer compiled with exception support (`-DWASI_SDK_EXCEPTIONS=ON`).
kolayne
added a commit
to kolayne/xgrammar-rs
that referenced
this pull request
Jun 3, 2026
Rewrites the library to rely on `cxx` rather than `autocxx` as the bindings back-end. Updates the building script accordingly. Note that a custom version of cxx is used: it includes the patch dtolnay/cxx#1715 for the `#[safe_shared_extern]` attribute. Motivation for the change: wasm32 support (trymirai#8). xgrammar-rs now compiles for `wasm32-wasip1` (and, supposedly, other wasi targets), though for now it still requires the `WASI_SYSROOT` environment variable to be set to the path to [wasi-sdk](https://github.com/WebAssembly/wasi-sdk) version 33 or newer compiled with exception support (`-DWASI_SDK_EXCEPTIONS=ON`).
kolayne
force-pushed
the
safe-shared-extern
branch
from
June 25, 2026 05:15
4896534 to
b367fd1
Compare
Adds the `safe_shared_extern` attribute.
The new attribute allows to define a shared struct that is automatically
verified for compatibility with the struct defined in C++.
This only works for structs that have no padding in their layout.
This also currently requires that the C++ struct is trivial, though
this may be relaxed.
The syntax is as follows:
```rust
mod ffi {
#[safe_shared_extern]
struct S {
pub a: i32,
pub b: i32,
}
extern "C++" {
include!("include.hpp");
// Note: no need to include `type S;` (the legacy unsafe syntax)
unsafe fn f() -> S;
}
}
```
TODO: add tests
Implement tests that various declaration mismatches are detected, as well as that the positive case works.
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.
Adds the
safe_shared_externattribute.The new attribute allows to define a shared struct that is automatically verified for compatibility with the struct defined in C++.
This only works for structs that have no padding in their layout. This also currently requires that the C++ struct is trivial, though this requirement may be relaxed.
The syntax is as follows:
Partially addresses #871.