Rust PubSub
A thread-safe, in-memory publish-subscribe library for Rust, designed for efficient inter-thread communication. It supports multiple publishers and subscribers across files, arbitrary message formats, customizable queue-full behavior, and callback processing in dedicated threads, ideal for modular Rust projects.
Features
- Thread-Safe: Built with
crossbeam-channelfor safe message passing between threads. - Multi-Publisher and Multi-Subscriber: Supports multiple publishers and subscribers for the same topic.
- Modular Design: Enables publishing and subscribing across files and modules via a singleton
PubSubinstance. - Arbitrary Message Formats: Supports any type implementing
Send + Sync + Clone + 'static, including custom structs. - Customizable Queue Behavior: Each subscriber can choose to overwrite old messages or stop writing when the queue is full.
- Callback Processing: Callbacks run in dedicated threads for non-blocking operation.
- Flexible Subscriptions: Offers manual receiving and callback-based subscriptions.
- Timeout Support: Provides blocking, non-blocking, and timeout-based operations.
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
Usage Examples
Below are examples demonstrating multi-publisher and multi-subscriber communication, arbitrary message formats (using a custom struct), customizable queue-full behavior, callback processing in dedicated threads, and modular design across files.
1. Multi-Publisher and Multi-Subscriber Across Files with Custom Struct
This example shows two publishers and two subscribers in separate files, using a custom struct CustomMessage as the message type. One subscriber uses a callback in a dedicated thread, highlighting modular design and arbitrary message formats.
src/main.rs
use TopicConfig;
use thread;
src/publisher.rs
use ;
// Custom message struct
src/subscriber.rs
use ;
use thread;
// Custom message struct (must be defined or imported)
2. Customizable Queue-Full Behavior
This example shows two subscribers with different queue-full behaviors: one overwrites old messages, the other stops writing when the queue is full.
use ;
use thread;
let pubsub = instance;
let topic = "queue_topic";
let overwrite_config = new; // Overwrite when full
let no_write_config = new; // Stop writing when full
// Subscriber 1: Overwrite mode
let receiver1 = pubsub.;
spawn;
// Subscriber 2: No-write mode
let receiver2 = pubsub.;
spawn;
// Publish messages to fill queues (could be in a separate file)
let topic_id = pubsub.create_publisher;
pubsub.publish;
pubsub.publish;
pubsub.publish; // Overwrite affects Subscriber 1 only
// Wait to observe
sleep;
3. Callback Subscription with Dedicated Thread
This example demonstrates a callback-based subscription, with the callback running in a dedicated thread. Any message type can be used.
use ;
use thread;
let pubsub = instance;
let topic = "callback_topic";
let config = new; // Overwrite when full
// Subscribe with callback (could be in a separate file)
pubsub.;
// Publish (could be in a separate file)
let topic_id = pubsub.create_publisher;
pubsub.publish;
// Wait to observe
sleep;
4. Publishing with Timeout
This example shows publishing with a timeout, receiving in a separate thread. Any message type can be used.
use ;
use thread;
let pubsub = instance;
let topic = "timeout_topic";
let config = new; // Stop writing when full
// Subscriber thread (could be in a separate file)
let receiver = pubsub.;
spawn;
// Publish with timeout (could be in a separate file)
let topic_id = pubsub.create_publisher;
pubsub.publish_with_timeout;
// Wait to observe
sleep;
5. Overwrite Mode with Full Queue
This example demonstrates publishing to a full queue with overwrite mode, receiving in a separate thread. Any message type can be used.
use ;
use thread;
let pubsub = instance;
let topic = "overwrite_topic";
let config = new; // Overwrite when full
// Subscriber thread (could be in a separate file)
let receiver = pubsub.;
spawn;
// Publish to fill and overwrite queue (could be in a separate file)
let topic_id = pubsub.create_publisher;
pubsub.publish;
pubsub.publish;
pubsub.publish; // Overwrites oldest message
// Wait to observe
sleep;
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
中文文档
Rust PubSub 是一个线程安全的、基于内存的发布-订阅库,专为 Rust 设计,旨在实现高效的线程间通信。它支持多个发布者和订阅者、任意消息格式、跨文件通信、每个订阅者可自定义队列满行为以及在专用线程中处理回调,非常适合模块化的 Rust 项目。
功能
- 线程安全:使用
crossbeam-channel实现线程间安全消息传递。 - 多发布者和多订阅者:支持同一主题的多个发布者和订阅者。
- 模块化设计:通过单例
PubSub实例,支持跨文件和模块的发布和订阅。 - 任意消息格式:支持任何实现
Send + Sync + Clone + 'static的类型,包括自定义结构体。 - 可自定义队列行为:每个订阅者可配置队列满时覆盖旧消息或停止写入。
- 回调处理:回调在单独线程中处理,确保非阻塞。
- 灵活订阅:支持手动接收和基于回调的订阅。
- 超时支持:提供阻塞、非阻塞和带超时操作。
安装
在 Cargo.toml 中添加:
[]
= "0.1.0"
使用示例
以下示例展示了多发布者和多订阅者通信、任意消息格式(使用自定义结构体)、可自定义队列满行为、在专用线程中处理回调以及跨文件模块化设计。
1. 跨文件的多发布者和多订阅者(使用自定义结构体)
此示例展示在不同文件中定义的两个发布者和两个订阅者,使用自定义结构体 CustomMessage 作为消息类型。一个订阅者使用回调,在内部专用线程中处理。
src/main.rs
use TopicConfig;
use thread;
src/publisher.rs
use ;
// 自定义消息结构体
src/subscriber.rs
use ;
use thread;
// 自定义消息结构体(必须定义或导入)
2. 可自定义队列满行为
此示例展示两个订阅者配置不同的队列满行为:一个覆盖旧消息,另一个队列满时停止写入。可使用任意消息类型。
use ;
use thread;
let pubsub = instance;
let topic = "queue_topic";
let overwrite_config = new; // 队列满时覆盖
let no_write_config = new; // 队列满时停止写入
// 订阅者 1:覆盖模式
let receiver1 = pubsub.;
spawn;
// 订阅者 2:停止写入模式
let receiver2 = pubsub.;
spawn;
// 发布消息以填充队列(可在单独文件中)
let topic_id = pubsub.create_publisher;
pubsub.publish;
pubsub.publish;
pubsub.publish; // 仅影响覆盖订阅者
// 等待观察
sleep;
3. 带专用线程的回调订阅
此示例展示回调订阅,回调在内部专用线程中处理。可使用任意消息类型。
use ;
use thread;
let pubsub = instance;
let topic = "callback_topic";
let config = new; // 队列满时覆盖
// 使用回调订阅(可在单独文件中)
pubsub.;
// 发布消息(可在单独文件中)
let topic_id = pubsub.create_publisher;
pubsub.publish;
// 等待观察
sleep;
4. 带超时的发布
此示例展示使用超时发布消息,接收在单独线程中进行。可使用任意消息类型。
use ;
use thread;
let pubsub = instance;
let topic = "timeout_topic";
let config = new; // 队列满时停止写入
// 订阅者线程(可在单独文件中)
let receiver = pubsub.;
spawn;
// 使用超时发布(可在单独文件中)
let topic_id = pubsub.create_publisher;
pubsub.publish_with_timeout;
// 等待观察
sleep;
5. 队列满时的覆盖模式
此示例展示队列满时使用覆盖模式,接收在单独线程中进行。可使用任意消息类型。
use ;
use thread;
let pubsub = instance;
let topic = "overwrite_topic";
let config = new; // 队列满时覆盖
// 订阅者线程(可在单独文件中)
let receiver = pubsub.;
spawn;
// 发布消息以填充并覆盖队列(可在单独文件中)
let topic_id = pubsub.create_publisher;
pubsub.publish;
pubsub.publish;
pubsub.publish; // 覆盖最早的消息
// 等待观察
sleep;
许可证
本项目采用 Apache 2.0 或 MIT 许可证,供您选择。