Skip to main content

MessageQueueSelectorFn

Type Alias MessageQueueSelectorFn 

Source
pub type MessageQueueSelectorFn = Arc<dyn Fn(&[MessageQueue], &dyn MessageTrait, &dyn Any) -> Option<MessageQueue> + Send + Sync>;
Expand description

Type-erased message queue selector function for storage and cross-boundary passing.

This type uses dynamic dispatch (Arc<dyn Fn>) and is suitable for scenarios where:

  • The selector needs to be stored in a struct field
  • The selector crosses async boundaries
  • The selector type cannot be determined at compile time

§Performance Note

This type incurs runtime overhead due to:

  • Dynamic dispatch (~5-10ns per call)
  • Arc reference counting
  • Type erasure with dyn Any

For best performance, prefer using generic parameters with the MessageQueueSelector trait in function signatures instead of this type alias.

§Example

use rocketmq_client_rust::MessageQueueSelectorFn;
use rocketmq_model::common::message::message_queue::MessageQueue;
use rocketmq_model::common::message::message_single::Message;
use std::sync::Arc;

let selector: MessageQueueSelectorFn = Arc::new(|mqs: &[MessageQueue], _msg, arg| {
    if mqs.is_empty() {
        return None;
    }
    let order_id = arg.downcast_ref::<i64>()?;
    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(&[], &msg, &7i64), None);

let mqs = vec![
    MessageQueue::from_parts("TopicTest", "broker-a", 0),
    MessageQueue::from_parts("TopicTest", "broker-a", 1),
];
assert_eq!(selector(&mqs, &msg, &1i64), Some(mqs[1].clone()));

// An argument that cannot downcast to `i64` still returns `None`.
assert_eq!(selector(&mqs, &msg, &"not-an-i64"), None);

Aliased Type§

pub struct MessageQueueSelectorFn { /* private fields */ }