Repository navigation
Add rust language binding - #246
Open
vijaiaeroastro wants to merge 16 commits into
Open
vijaiaeroastro wants to merge 16 commits into
vijaiaeroastro wants to merge 16 commits into
Conversation
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
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.
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.tomlandsrc/lib.rs(no dependencies besides crates of imported components), plus a small example underExamples/RustResult<T>, instances release onDropDerefto the parent class, and run-time type checks (is,cast,downcast,into_kind) based on the class type id#[repr(C)], or packed where C would add padding; matrices indexed[column][row]like the other bindingsinject_<component>helper (the raw symbol lookup call isunsafe)Send, andSynconly forstrictclassesunsafe<name>_rawfunction. 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 asunsafefunctions only.indentationand-suppressexamplesare honoredNotes 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)<binding>added to the example IDLs, with generated bindings and hand-written examples. The example scripts are called from thebuild.shof each example.Build/Dockerfile: installs Rust, and downloads Free Pascal from downloads.freepascal.org (the SourceForge download was dropped mid-transfer during testing)Testing
cargo clippy. They build from Rust 1.57 and without warnings from 1.65 (rust-version = "1.65").cargo check --target x86_64-pc-windows-gnu)Known limits
unsafeenumarrayis not generated (no binding generates it)Examples/OptionalClassis not covered, as its IDL already fails ACT's check ondevelopExamples/ThreadSafety/build.shis disabled upstream and calls a script that does not exist; the Rust example for it was run manuallyUnrelated 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)