pub trait MessageQueueSelector<M: MessageTrait, A>: Send + Sync {
// Required method
fn select(
&self,
mqs: &[MessageQueue],
msg: &M,
arg: &A,
) -> Option<MessageQueue>;
}Expand description
A trait for selecting a message queue from a list of available queues.
This trait provides a zero-cost abstraction for queue selection logic using compile-time monomorphization. Implement this trait for your custom selector logic, or use closures directly as they automatically implement this trait.
§Type Parameters
M- Message type that implementsMessageTraitA- Argument type for custom selection logic
§Performance
This trait uses compile-time generics, allowing the compiler to:
- Fully inline the selector function
- Eliminate dynamic dispatch overhead
- Perform aggressive optimizations
§Example
use rocketmq_client_rust::MessageQueueSelector;
use rocketmq_model::common::message::message_queue::MessageQueue;
use rocketmq_model::common::message::message_single::Message;
// Closures automatically implement MessageQueueSelector. Order IDs are signed, so guard
// against an empty queue list before indexing and use `rem_euclid` so a negative order ID
// still maps into the valid queue range instead of producing a negative remainder.
let selector = |mqs: &[MessageQueue], _msg: &Message, order_id: &i64| {
if mqs.is_empty() {
return None;
}
let index = order_id.rem_euclid(mqs.len() as i64) as usize;
mqs.get(index).cloned()
};
let msg = Message::builder()
.topic("TopicTest")
.body_slice(b"body")
.build_unchecked();
assert_eq!(selector.select(&[], &msg, &7), None);
let mqs = vec![
MessageQueue::from_parts("TopicTest", "broker-a", 0),
MessageQueue::from_parts("TopicTest", "broker-a", 1),
MessageQueue::from_parts("TopicTest", "broker-a", 2),
];
assert_eq!(selector.select(&mqs, &msg, &1), Some(mqs[1].clone()));
assert_eq!(selector.select(&mqs, &msg, &-1), Some(mqs[2].clone()));
let first = selector.select(&mqs, &msg, &i64::MIN);
let second = selector.select(&mqs, &msg, &i64::MIN);
assert_eq!(first, second);
assert!(first.is_some_and(|mq| mqs.contains(&mq)));Required Methods§
Sourcefn select(&self, mqs: &[MessageQueue], msg: &M, arg: &A) -> Option<MessageQueue>
fn select(&self, mqs: &[MessageQueue], msg: &M, arg: &A) -> Option<MessageQueue>
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementors§
impl<F, M, A> MessageQueueSelector<M, A> for F
Implement MessageQueueSelector for all compatible closures and functions.
This allows closures to be used directly as selectors without explicit trait implementation.