Skip to content

Comments

feat: add send feature flag for thread-safe Lua state#30

Open
danielsreichenbach wants to merge 3 commits intomainfrom
feature/send-compat
Open

feat: add send feature flag for thread-safe Lua state#30
danielsreichenbach wants to merge 3 commits intomainfrom
feature/send-compat

Conversation

@danielsreichenbach
Copy link
Member

Summary

  • Add cargo feature send that makes Lua implement Send
  • Enables use in multi-threaded embedding frameworks like bevy_mod_scripting
  • Feature is off by default, no changes to existing behavior

Changes

When send is enabled:

  • ProtoRef type alias switches from Rc<Proto> to Arc<Proto>
  • UserDataBox type alias switches from Box<dyn Any> to Box<dyn Any + Send>
  • GcRef<T> gains Send + Sync (index-only, no pointer data)
  • IoFile and DynLib gain Send impls
  • unsafe impl Send for Lua is provided

21 files changed across the codebase to use the new type aliases instead of direct Rc<Proto> references.

Closes #29

Test plan

  • cargo fmt -- --check passes
  • cargo clippy --all-targets passes (0 warnings)
  • cargo clippy --all-targets --features send passes (0 warnings)
  • cargo test passes (1323 tests)
  • cargo test --features send passes (1324 tests, +1 lua_is_send compile-time assertion)
  • cargo doc --no-deps passes

Introduce a `send` cargo feature that makes `Lua` implement `Send`,
enabling use in multi-threaded contexts like bevy_mod_scripting.

When enabled:
- `ProtoRef` switches from `Rc<Proto>` to `Arc<Proto>`
- `UserDataBox` switches from `Box<dyn Any>` to `Box<dyn Any + Send>`
- `GcRef<T>` gains `Send + Sync` (index-only, no pointer data)
- `IoFile` and `DynLib` gain `Send` impls
- `unsafe impl Send for Lua` is provided

All existing tests pass in both configurations. The send feature adds
one compile-time test asserting `Lua: Send`.

Closes #29
Add public API methods needed by bevy_mod_scripting integration:
- Table::from_gc_ref, Function::from_gc_ref, AnyUserData::from_gc_ref
- Lua::table_next for table iteration
- Lua::val_as_bytes for string value access
Provides a public API for borrowing typed data from AnyUserData
handles without exposing the internal LuaState. Needed by external
integrations that work with userdata through the high-level Lua API.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

feat: add send feature flag for thread-safe Lua state

1 participant