Skip to content

Add rust language binding - #246

Open
vijaiaeroastro wants to merge 16 commits into
developfrom
vijaiaeroastro/rust-port
Open

vijaiaeroastro wants to merge 16 commits into
developfrom
vijaiaeroastro/rust-port

Conversation

@vijaiaeroastro

@vijaiaeroastro vijaiaeroastro commented Dec 5, 2026 •

Copy link
Copy Markdown
Collaborator

This PR is copied from my personal fork and then further strengthened and improved.

It adds a Rust binding generator to ACT. It generates a Rust crate that loads a component at run time (dlopen/LoadLibrary), like the other dynamic bindings. Rust is a binding language only; there is no Rust implementation stub (If there's a demand for that, its definitely doable).

What it generates

  • Bindings/Rust/Cargo.toml and src/lib.rs (no dependencies besides crates of imported components), plus a small example under Examples/Rust
  • Idiomatic Rust: snake case names, every call returns Result<T>, instances release on Drop
  • Classes with Deref to the parent class, and run-time type checks (is, cast, downcast, into_kind) based on the class type id
  • Structs as #[repr(C)], or packed where C would add padding; matrices indexed [column][row] like the other bindings
  • Enums are checked when they come back from the library, also inside structs
  • Component injection: safe inject_<component> helper (the raw symbol lookup call is unsafe)
  • Thread safety options: instances are Send, and Sync only for strict classes
  • Callbacks: methods with a callback and a user data pointer take closures. The variant with C ABI callbacks is kept as an unsafe <name>_raw function. A panic in a closure aborts, as it cannot unwind through the library. Callbacks that cannot be mapped (no user data pointer, or arrays to fill) are available as unsafe functions only.
  • indentation and -suppressexamples are honored

Notes are in Documentation/Rust.md.

Changes outside the generator

  • Source/automaticcomponenttoolkit.go, Source/ACT.xsd, Build/build.sh, Build/build.bat: Changes necessary to hook the generator into existing files (and also relevant build scripts)
  • Rust <binding> added to the example IDLs, with generated bindings and hand-written examples. The example scripts are called from the build.sh of each example.
  • Build/Dockerfile: installs Rust, and downloads Free Pascal from downloads.freepascal.org (the SourceForge download was dropped mid-transfer during testing)
  • README: language list, feature matrix row, link to the Rust notes

Testing

  • All Rust example scripts build and run against both the C++ and the Pascal libraries (macOS arm64, and the CI Docker image on Linux x86_64), as root and as a non-root user
  • Generated crates are clean under cargo clippy. They build from Rust 1.57 and without warnings from 1.65 (rust-version = "1.65").
  • The Windows code path is compile-checked only (cargo check --target x86_64-pc-windows-gnu)
  • Also checked against lib3mf 2.6.0 as a large real-world consumer: round trips, beam lattice, callbacks, secure content, boolean and displacement extensions

Known limits

  • Callbacks without a user data pointer, and callbacks that must fill arrays, stay unsafe
  • enumarray is not generated (no binding generates it)
  • Examples/OptionalClass is not covered, as its IDL already fails ACT's check on develop
  • Examples/ThreadSafety/build.sh is disabled upstream and calls a script that does not exist; the Rust example for it was run manually

Unrelated changes

Since Docker build kept getting timeout while attempting to download free pascal compiler from source forge, i swapped the source to the official freepascal website itself (https://www.freepascal.org/download.html)

Generates a Rust crate (dlopen-based, no build-time dependency on the
component) from an ACT component description.

Working, tested against the example components and lib3mf 2.6.0:
- classes with inheritance, Deref to the parent, is/downcast
- strings, basic/struct arrays, structs (packed, [column][row] matrices), enums
- callbacks, error codes and messages, class type ids
- imported components (injection) as path-dependency crates
- Send/Sync classes and instance locking for threadsafetyoption
- generated example program and Cargo manifests
- cast::<T>() narrows an instance to a derived class without consuming it
- from_borrowed() wraps a handle the library passes to a callback
- Debug for Error prints the readable message
…llback docs

- into_kind() on classes with descendants returns a <Class>Kind enum for match
- structs are plain repr(C) when C adds no padding (fields can be borrowed),
  repr(C, packed) otherwise; compile-time size checks for every struct
- document that callbacks must not panic
- Rust binding entry in the example IDLs
- generated Rust bindings and examples for Calculator, Injection (with
  injected Numbers), Primes, RTTI, UnitTest and ThreadSafety
- Examples/Rust/build.sh for each, run from the example build scripts against
  the C++ and Pascal libraries
- Rust toolchain in the CI Dockerfile, Rust row in the README
The examples use only the example libraries of ACT and run against both the
C++ and the Pascal implementation:
- Calculator, Calculation (injection of Numbers), Primes (progress callback)
- RTTI (class hierarchy, is/downcast, error codes), UnitTest (all scalar
  types, enum, strings), ThreadSafety (shared strict instance, 5 threads)
- Narrow Sync to strict classes, mark callback/pointer/symbol-lookup entry points unsafe
- Add safe inject_<component> helper that keeps the injected wrapper alive
- Validate enums inside structs through a raw twin instead of mem::zeroed
- Clippy clean: matches!, annotated transmutes, no needless Ok(?)
- sourceforge dropped the connection mid-download
- fail on HTTP errors, retry and resume
- Methods with callbacks and user data take closures; the C ABI variant is kept as an unsafe _raw function
- A panic in a closure aborts instead of unwinding into the library
- Go unit tests for the generator, including a generated crate compiled with clippy
- Windows: no unused CStr import; Cargo.toml gets a description
- Usage, types, classes, threads, callbacks, injection
- README: link and tested platforms
- Go test builds a C library and runs the generated crate against it: closures, enums in structs, invalid values, panic abort
- rust-version 1.65 in the generated Cargo.toml (warning free from 1.65, builds from 1.57)
- other generators are tested through the Examples only
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.

1 participant