example_component is a Rust component example based on RT-Thread, demonstrating how to implement, register, and manage Rust components in RT-Thread systems. This project adopts a unified component registration architecture that avoids component duplicate registration issues and supports conditional compilation based on RT-Thread configuration.
example_component/
├── SConscript # SCons build script
├── component_registry/ # Unified component registry
│ ├── Cargo.toml # Registry configuration
│ └── src/
│ └── lib.rs # Registry implementation
├── log/ # Log component example
│ ├── Cargo.toml # Log component configuration
│ └── src/
│ ├── lib.rs # Log component implementation
│ └── logging.rs # Logging functionality module
└── tools/ # Build tools
├── build_component.py # Component build tool
└── feature_config_component.py # Feature configuration
component_registry, avoiding conflicts caused by multiple component duplicate registrationscomponent_registry is the core of the entire architecture, responsible for unified management of all component registrations.
rust_component_registry as the unified registration entry for all componentsThe log component provides Rust-style logging functionality, including multiple log levels and command-line interface.
log_info, log_warn, log_errorenable-log feature is enabled// Using log macros in code
info!("This is an info log");
warn!("This is a warning log");
error!("This is an error log");
# Using in RT-Thread command line
msh> log_info # Print info log
msh> log_warn # Print warning log
msh> log_error # Print error log
The mapping from RT-Thread configuration to Rust features is defined in tools/feature_config_component.py:
CONFIG_COMPONENT_FEATURE_MAP = {
'RUST_LOG_COMPONENT': {
'feature': 'enable-log',
'dependency': 'em_component_log',
'description': 'Enable Rust log component integration'
}
}
To enable the log component, set in RT-Thread configuration:
// Set through menuconfig
Enable Rust component support -> Auto-initialize Rust log component
Create Component Directory
cargo new --lib example_component/new_component
cd example_component/new_component
Create Cargo.toml
[package]
name = "em_component_new"
version = "0.1.0"
edition = "2021"
[lib]
name = "em_component_new"
crate-type = ["staticlib"]
[dependencies]
rt-rust = { path = "../../rust" }
macro_main = { path = "../../rust/macro-main" }
[features]
default = []
enable-new = []
Implement Component Functionality
// src/new.rs
// New component implementation code
// src/lib.rs
#![no_std]
// Export required libraries
Update Feature Configuration
# Add to tools/feature_config_component.py
CONFIG_COMPONENT_FEATURE_MAP = {
# ... existing configuration ...
'RUST_NEW_COMPONENT': {
'feature': 'enable-new',
'dependency': 'em_component_new',
'description': 'Enable new component integration'
}
}
Update Registry
# Add to component_registry/Cargo.toml
[dependencies]
em_component_new = { path = "../new_component", optional = true }
[features]
enable-new = ["em_component_new", "em_component_new/enable-new"]
// Register in component_registry/src/lib.rs
#[cfg(feature = "enable-new")]
use em_component_new;
fn component_registry_main() {
#[cfg(feature = "enable-new")]
{
println!("Component registry: New component enabled");
}
// ... other components ...
}
#[cfg(feature = "...")]component_registry manage uniformlyCompilation Failure
riscv64imac-unknown-none-elf) is installedFeatures Not Enabled
feature_config_component.pyLinking Errors
Cargo.tomlRT-Thread Config
↓
Feature Mapping
↓
Rust Features
↓
Component Registry ← Individual Components
↓
RT-Thread System