Skip to content

Conversation

@soburi
Copy link
Member

@soburi soburi commented Nov 2, 2025

We will gradually add a Rust compatible API implementation conforming to Apache-2.
This PR adds an alternative implementation of Common.cpp.

@soburi soburi added the DNM This PR should not be merged (Do Not Merge) label Nov 2, 2025
@soburi soburi changed the title rust: Add a Rust compatible API implementation. [DNM] rust: Add a Rust compatible API implementation. Nov 2, 2025
@soburi soburi force-pushed the rust_impl branch 6 times, most recently from 7a556b5 to f86a047 Compare November 2, 2025 19:05
@soburi soburi changed the title [DNM] rust: Add a Rust compatible API implementation. rust: Add a Rust compatible API implementation. Nov 3, 2025
@soburi soburi marked this pull request as ready for review November 3, 2025 01:08
Copy link
Contributor

@pillo79 pillo79 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, looking forward to see this grow! 🥳 🤗

Kconfig Outdated
imply CBPRINTF_FP_SUPPORT
imply RING_BUFFER
select UART_INTERRUPT_DRIVEN
select RUST
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should currently be an optional feature, to avoid adding the strong dependency right away.

run: |
git clone https://github.com/arduino/ArduinoCore-API.git $MODULE_PATH/../ArduinoCore-API
cp -rfp $MODULE_PATH/../ArduinoCore-API/api $MODULE_PATH/cores/arduino/
apt install rustup
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recommended way to install Rust is via the rustup.rs installer script, Ubuntu's rustup package from apt is often outdated and may not work correctly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also,
All initialization is lumped into one "Initialize" step mixing unrelated concerns (cloning API, copying files, Rust setup)...
Better structure:

  - name: Clone ArduinoCore-API
[...]
 - name: Install Rust toolchain
[...]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thank you for your comment.

rustup target add thumbv6m-none-eabi
rustup target add thumbv7em-none-eabihf
rustup target add thumbv7em-none-eabi
rustup target add thumbv7m-none-eabi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Installing targets one-by-one is slower. Rustup supports installing multiple targets in one command

rustup target add thumbv6m-none-eabi
rustup target add thumbv7em-none-eabihf
rustup target add thumbv7em-none-eabi
rustup target add thumbv7m-none-eabi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rustup target add thumbv7m-none-eabi
rustup target add thumbv6m-none-eabi thumbv7em-none-eabihf thumbv7em-none-eabi thumbv7m-none-eabi

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

let num = x.wrapping_sub(in_min).wrapping_mul(out_max.wrapping_sub(out_min));
let den = in_max.wrapping_sub(in_min);
// Note: To keep compatibility, the panic when den=0 is left as is.
num / den.wrapping_add(out_min)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to operator precedence, this evaluates as:

  num / (den + out_min)  // Division happens AFTER adding out_min to den!                                                                                                                                          

Original C++ code:

  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;

Can we just follow similar arithmetic syntax in rust too, the code using wrapping_* is making the logic really confusing to understand, and with rust already being a new addition I feel like we should make an effort to keep things simple where possible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is not a "new feature" but a reimplementation of the original implementation, we believe compatibility should be maintained.

From another perspective, this implementation is an internal implementation of the C++ API,
and therefore does not take advantage of Rust's features.
To take advantage of Rust's features would require a complete overhaul of the API set, so we think it's better to leave it this way for now.
It would also be good to have an option for more Rust-like behavior in the future.

Kconfig Outdated
Comment on lines 20 to 22
config USE_ARDUINO_API_RUST_IMPLEMENTATION
bool "Use Rust implementation API core"
default y
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to be nitpicky, but I asked this before and I'm still confused. 🙂

Is Rust expected to be installed by anyone trying to use the core at this stage?
If not, I would expect this to be manually set somehow (via the west command line, using snippets, etc) and not being default y-ed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 @pillo79
I would prefer this be optional and not default y too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. This is still in an experimental state.
Removed the default y.

We will gradually add a Rust compatible API implementation conforming
to Apache-2.
This PR adds an alternative implementation of Common.cpp.

Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
Adding Rust architecture modules for ensure compilation.

Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

DNM This PR should not be merged (Do Not Merge)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants