| Crates.io | hsipc |
| lib.rs | hsipc |
| version | 0.1.3 |
| created_at | 2025-07-09 15:57:03.542105+00 |
| updated_at | 2025-07-09 15:57:03.542105+00 |
| description | High-performance inter-process communication framework in Rust |
| homepage | https://github.com/loyalpartner/hsipc |
| repository | https://github.com/loyalpartner/hsipc |
| max_upload_size | |
| id | 1745057 |
| size | 140,397 |
一个基于 ipmb 构建的声明式、类型安全的进程间通信框架,提供请求/响应和发布/订阅模式,支持同步和异步操作。
hsipc/
├── hsipc/ # 核心库
├── hsipc-macros/ # 过程宏
├── examples/ # 示例代码
│ ├── request_response/ # 基础请求/响应服务
│ ├── pubsub_events/ # 事件发布和订阅
│ └── trait_based_service/ # Trait-based 服务示例
└── README.md
添加 hsipc 到你的 Cargo.toml:
[dependencies]
hsipc = { path = "./hsipc" }
tokio = { version = "1.0", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
服务定义:
use hsipc::{rpc, method, ProcessHub, Result};
// 定义 RPC 服务接口
#[rpc(server, client, namespace = "calculator")]
pub trait Calculator {
#[method(name = "add")]
async fn add(&self, params: (i32, i32)) -> Result<i32>;
#[method(name = "multiply")]
async fn multiply(&self, params: (i32, i32)) -> Result<i32>;
}
// 服务实现
struct CalculatorImpl;
#[hsipc::async_trait]
impl Calculator for CalculatorImpl {
async fn add(&self, params: (i32, i32)) -> Result<i32> {
Ok(params.0 + params.1)
}
async fn multiply(&self, params: (i32, i32)) -> Result<i32> {
Ok(params.0 * params.1)
}
}
#[tokio::main]
async fn main() -> Result<()> {
let hub = ProcessHub::new("calculator").await?;
// 注册服务 - 自动生成 CalculatorService
let calculator = CalculatorImpl;
hub.register_service(CalculatorService::new(calculator)).await?;
// 自动生成的类型安全客户端
let client = CalculatorClient::new(hub.clone());
let result = client.add((10, 20)).await?;
println!("10 + 20 = {}", result);
Ok(())
}
发布者:
use hsipc::{ProcessHub, Result};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
pub struct TemperatureEvent {
pub sensor_id: String,
pub value: f64,
}
#[tokio::main]
async fn main() -> Result<()> {
let hub = ProcessHub::new("sensor").await?;
let event = TemperatureEvent {
sensor_id: "room_001".to_string(),
value: 25.5,
};
hub.publish("sensor/temperature", event).await?;
Ok(())
}
对于不使用 async/await 的应用程序:
use hsipc::SyncProcessHub;
fn main() -> hsipc::Result<()> {
let hub = SyncProcessHub::new("my_process")?;
// 同步服务调用
let result: i32 = hub.call("add", AddRequest { a: 5, b: 3 })?;
// 同步事件发布
hub.publish("sensor/temperature", TemperatureEvent {
sensor_id: "room_001".to_string(),
value: 22.0,
})?;
Ok(())
}
发布/订阅系统支持 MQTT 风格的主题模式:
sensor/temperature - 精确匹配sensor/+ - 单级通配符 (匹配 sensor/temperature, sensor/humidity)sensor/# - 多级通配符 (匹配 sensor/room1/temperature, sensor/room2/humidity/current)# RPC 系统完整演示(推荐)
cargo run --example rpc_system_demo demo
# RPC 服务示例
cargo run --example trait_based_service demo
# 基础请求/响应示例
# 终端 1 - 启动服务器
cargo run --example request_response server
# 终端 2 - 启动客户端
cargo run --example request_response client
# 发布/订阅示例
# 终端 1 - 启动订阅者
cargo run --example pubsub_events subscriber
# 终端 2 - 启动发布者
cargo run --example pubsub_events publisher
hsipc 采用分层架构:
这是一个功能完善的 IPC 框架实现,基于 ipmb 构建。当前实现包括:
#[rpc(server, client, namespace = "name")] 宏基于 ipmb,hsipc 已验证性能:
通过 criterion 基准测试验证。运行 make bench-quick 查看详细结果。
PendingSubscriptionSink 订阅机制| 特性 | RPC v2 (当前) | 描述 |
|---|---|---|
| 类型安全 | ✅ 编译时检查 | trait 定义确保类型一致性 |
| 自动生成 | ✅ 服务+客户端 | 自动生成 ServiceName{Service,Client} |
| 异步支持 | ✅ 默认异步 | 支持 sync 标记的同步方法 |
| 订阅机制 | ✅ 内置支持 | 自动插入 PendingSubscriptionSink |
| IDE 支持 | ✅ 完整 | 完整的代码补全和类型提示 |
| 测试友好 | ✅ 优秀 | 易于测试的 trait 基础架构 |
根据以下任一许可证授权:
任选其一。
欢迎贡献!请随时提交 Pull Request。