8 releases
new 0.1.7 | Apr 17, 2025 |
---|---|
0.1.6 | Apr 15, 2025 |
#610 in Rust patterns
661 downloads per month
57KB
820 lines
Rust-Interface-Wrappers-Spring-25
Rust Foreign Function Interface Wrapper Crate RI: Daniel Dusharm ddusharm@asrcfederal.com
External bindings make the (computer) world go round. For example, Python AI/ML projects that use performance-critical or efficient libraries do so by leveraging foreign function interfaces (FFI) to call native code. This project demonstrates the development of a wrapper crate in Rust using std::ffi
to expose the functionality of an external C library while ensuring type safety. Students will gain hands-on experience with Rust fundamentals such as memory safety, ownership, and FFI integration.
Project Overview
This project implements a calculator library that demonstrates Rust-C FFI integration. It consists of a C library for core calculations and a Rust wrapper providing a safe interface through a command-line interface (CLI).
Key Features
- Infix to RPN (Reverse Polish Notation) conversion
- Mathematical expression evaluation
- RESTful API interface
- Memory-safe FFI implementation
- Robust error handling
- Support for complex mathematical expressions
- History saves all user input with answers or errors
Installation
Prerequisites
- Rust (latest stable)
- C compiler (gcc/clang)
- Cargo
Usage
Running the Application
To start the calculator application, run the following command:
cargo run --bin main
Project Alignment
This implementation aligns with the project goals in several ways:
-
FFI Integration
- Uses
std::ffi
for C interop - Handles C strings and data types
- Manages memory across language boundaries
- Uses
-
Memory Safety
- Safe wrapper around unsafe C code
- Proper memory management
- Rust ownership rules enforcement
-
Error Handling
- Comprehensive error types
- Safe error propagation
- User-friendly error messages
-
Type Safety
- Strong type checking
- Safe type conversions
- Rust type system utilization
Technical Details
Memory Management
The project handles memory safety through:
- RAII principles in Rust
- Careful C memory allocation/deallocation
- Smart pointer usage
- Automatic cleanup on scope exit
Error Handling
Error types include:
- Division by zero
- Invalid operators
- Stack underflow
- Memory errors
- Undefined variables
- Stack maximum
- Expretion Length Maximum
Type Conversions
Handles conversions between:
- Rust strings ↔ C strings
- Rust numbers ↔ C numbers
- Complex data structures
- Error codes
Testing
Run all tests
cargo test
Run a specific test
cargo test --test <test_name>
Limitations and Future Work
Current limitations:
- Basic operator set
Planned improvements:
- Scientific notation parsing for negative numbers
- Dynamic memory allocation
- Additional mathematical functions
- Variable support
- Function definitions
Additional Enhancements
-
Additional Mathematical Functions
- Trigonometric functions (sin, cos, tan)
- Logarithmic functions (ln, log)
- Constants (pi, e)
-
Expression Optimization
- Constant folding
- Expression simplification
- Performance improvements
Testing Requirements
- Test Coverage
- Add unit tests for negative numbers
- Add tests for Unicode handling
- Add scientific notation tests
- Add stress tests for large expressions
- Add history testing with rezults and errors
Documentation Updates
- API Documentation
- Document all supported operators
- Add examples for all edge cases
- Update error messages
Performance Improvements
- Optimization Opportunities
- Memory allocation optimization
- Parser performance improvements
- Error handling efficiency
- String handling optimization
License
This project is part of academic coursework and is subject to course guidelines.
Acknowledgments
- Daniel Dusharm (Project Advisor)
- ASRC Federal
- Dr. Neil Toporski (Course Instructor)
- Project Team Members
Dependencies
~0.4–315KB