|
|
4 周之前 | |
|---|---|---|
| .. | ||
| README.md | 4 周之前 | |
| README_zh.md | 4 周之前 | |
rt_macros is a collection of Rust procedural macros for RT-Thread. It helps export Rust functions as system entry points, initialization entries, or shell commands in a no_std environment, so they integrate cleanly with RT-Thread’s startup and command mechanisms.
In no_std mode, the traditional main function is not directly available. rt_macros generates C-ABI-compatible wrapper entries and the required link-section items so RT-Thread can discover and invoke the corresponding Rust logic during startup or shell command scanning.
rt_thread_main!: Marks the Rust main-thread entry.
fn() (no parameters, no return value).const, unsafe, async, varargs; no explicit ABI; no generics; default visibility (non-pub).rt_component_export!: Exports a component initialization entry.
fn().name = "..." (used to generate internal symbol names)..rti_fn.4, executed during the component initialization phase.rt_app_export!: Exports an application initialization entry.
fn().name = "..."..rti_fn.6, executed during the application initialization phase.msh_cmd_export!: Exports an RT-Thread shell command.
fn(args: vec::IntoIter<rt_rust::param::ParamItem>).name (required, command name), desc (optional, command description).FSymTab.\0-terminated byte arrays placed into .rodata.name.extern "C" wrapper (argc, argv), converts argv to Vec<ParamItem>, and calls the original Rust command function..rti_fn.4.rti_fn.6FSymTab.rodata.nameDuring startup or shell scanning, RT-Thread traverses these sections to register or invoke the corresponding Rust logic.
use rt_macros::rt_thread_main;
#[rt_thread_main]
fn main() {
// Main thread logic here
}
use rt_macros::rt_component_export;
#[rt_component_export(name = "rust_component_registry")]
fn my_component_init() {
// Component init logic
}
use rt_macros::rt_app_export;
#[rt_app_export(name = "rust_app_example")]
fn my_app_init() {
// Application init logic
}
use rt_macros::msh_cmd_export;
#[msh_cmd_export(name = "hello", desc = "Say hello")]
fn hello_cmd(args: vec::IntoIter<rt_rust::param::ParamItem>) {
// Handle args and print output, etc.
}
When calling exported Rust entries from C, declare the prototype and use extern "C" for the calling convention, for example:
extern "C" void rust_function_name(void);
The command export macro generates a wrapper with (argc, argv); RT-Thread’s command system calls this wrapper and passes the arguments to the original Rust function.
fn() or a specific parameter shape). Adjust the function to meet the requirements above.pub) visibility to satisfy macro constraints.alloc dependency: the shell command macro uses alloc::vec::Vec, ensure an allocator is available at runtime (RT-Thread usually provides one).