1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2023 The RocketMQ Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use Arc;
use MessageQueue;
use MessageTrait;
/// 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 implements `MessageTrait`
/// * `A` - 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
///
/// ```no_run
/// use rocketmq_client_rust::producer::message_queue_selector::MessageQueueSelector;
/// use rocketmq_common::common::message::message_queue::MessageQueue;
/// use rocketmq_common::common::message::message_single::Message;
///
/// // Closures automatically implement MessageQueueSelector
/// let selector = |mqs: &[MessageQueue], _msg: &Message, order_id: &i64| {
/// let index = (*order_id % mqs.len() as i64) as usize;
/// mqs.get(index).cloned()
/// };
/// ```
/// Implement MessageQueueSelector for all compatible closures and functions.
///
/// This allows closures to be used directly as selectors without explicit trait implementation.
/// 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
///
/// ```no_run
/// use rocketmq_client_rust::producer::message_queue_selector::MessageQueueSelectorFn;
/// use std::sync::Arc;
///
/// let selector: MessageQueueSelectorFn = Arc::new(|mqs, _msg, arg| {
/// let order_id = arg.downcast_ref::<i64>()?;
/// let index = (*order_id % mqs.len() as i64) as usize;
/// mqs.get(index).cloned()
/// });
/// ```
pub type MessageQueueSelectorFn =
;