-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathinto_node_subscription_callback.rs
More file actions
174 lines (156 loc) · 6.11 KB
/
Copy pathinto_node_subscription_callback.rs
File metadata and controls
174 lines (156 loc) · 6.11 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use rosidl_runtime_rs::Message;
use crate::{
AnySubscriptionCallback, MessageInfo, NodeSubscriptionCallback, ReadOnlyLoanedMessage,
};
/// A trait for regular callbacks of subscriptions.
///
/// Subscription callbacks support six signatures:
/// - [`FnMut`] ( `Message` )
/// - [`FnMut`] ( `Message`, [`MessageInfo`] )
/// - [`FnMut`] ( [`Box`]<`Message`> )
/// - [`FnMut`] ( [`Box`]<`Message`>, [`MessageInfo`] )
/// - [`FnMut`] ( [`ReadOnlyLoanedMessage`]<`Message`> )
/// - [`FnMut`] ( [`ReadOnlyLoanedMessage`]<`Message`>, [`MessageInfo`] )
pub trait IntoNodeSubscriptionCallback<T, Args>: Send + 'static
where
T: Message,
{
/// Converts the callback into an enum.
///
/// User code never needs to call this function.
fn into_node_subscription_callback(self) -> AnySubscriptionCallback<T, ()>;
}
impl<T, Func> IntoNodeSubscriptionCallback<T, (T,)> for Func
where
T: Message,
Func: FnMut(T) + Send + 'static,
{
fn into_node_subscription_callback(self) -> AnySubscriptionCallback<T, ()> {
NodeSubscriptionCallback::SyncRegular(Box::new(self)).into()
}
}
impl<T, Func> IntoNodeSubscriptionCallback<T, (T, MessageInfo)> for Func
where
T: Message,
Func: FnMut(T, MessageInfo) + Send + 'static,
{
fn into_node_subscription_callback(self) -> AnySubscriptionCallback<T, ()> {
NodeSubscriptionCallback::SyncRegularWithMessageInfo(Box::new(self)).into()
}
}
impl<T, Func> IntoNodeSubscriptionCallback<T, (Box<T>,)> for Func
where
T: Message,
Func: FnMut(Box<T>) + Send + 'static,
{
fn into_node_subscription_callback(self) -> AnySubscriptionCallback<T, ()> {
NodeSubscriptionCallback::SyncBoxed(Box::new(self)).into()
}
}
impl<T, Func> IntoNodeSubscriptionCallback<T, (Box<T>, MessageInfo)> for Func
where
T: Message,
Func: FnMut(Box<T>, MessageInfo) + Send + 'static,
{
fn into_node_subscription_callback(self) -> AnySubscriptionCallback<T, ()> {
NodeSubscriptionCallback::SyncBoxedWithMessageInfo(Box::new(self)).into()
}
}
impl<T, Func> IntoNodeSubscriptionCallback<T, (ReadOnlyLoanedMessage<T>,)> for Func
where
T: Message,
Func: FnMut(ReadOnlyLoanedMessage<T>) + Send + 'static,
{
fn into_node_subscription_callback(self) -> AnySubscriptionCallback<T, ()> {
NodeSubscriptionCallback::SyncLoaned(Box::new(self)).into()
}
}
impl<T, Func> IntoNodeSubscriptionCallback<T, (ReadOnlyLoanedMessage<T>, MessageInfo)> for Func
where
T: Message,
Func: FnMut(ReadOnlyLoanedMessage<T>, MessageInfo) + Send + 'static,
{
fn into_node_subscription_callback(self) -> AnySubscriptionCallback<T, ()> {
NodeSubscriptionCallback::SyncLoanedWithMessageInfo(Box::new(self)).into()
}
}
#[cfg(test)]
mod tests {
use super::*;
type TestMessage = ros_env::test_msgs::msg::BoundedSequences;
#[test]
fn callback_conversion() {
let cb = |_msg: TestMessage| {};
assert!(matches!(
cb.into_node_subscription_callback(),
AnySubscriptionCallback::Node(NodeSubscriptionCallback::<TestMessage>::SyncRegular(_)),
));
let cb = |_msg: TestMessage, _info: MessageInfo| {};
assert!(matches!(
cb.into_node_subscription_callback(),
AnySubscriptionCallback::Node(
NodeSubscriptionCallback::<TestMessage>::SyncRegularWithMessageInfo(_)
)
));
let cb = |_msg: Box<TestMessage>| {};
assert!(matches!(
cb.into_node_subscription_callback(),
AnySubscriptionCallback::Node(NodeSubscriptionCallback::<TestMessage>::SyncBoxed(_)),
));
let cb = |_msg: Box<TestMessage>, _info: MessageInfo| {};
assert!(matches!(
cb.into_node_subscription_callback(),
AnySubscriptionCallback::Node(
NodeSubscriptionCallback::<TestMessage>::SyncBoxedWithMessageInfo(_)
),
));
let cb = |_msg: ReadOnlyLoanedMessage<TestMessage>| {};
assert!(matches!(
cb.into_node_subscription_callback(),
AnySubscriptionCallback::Node(NodeSubscriptionCallback::<TestMessage>::SyncLoaned(_)),
));
let cb = |_msg: ReadOnlyLoanedMessage<TestMessage>, _info: MessageInfo| {};
assert!(matches!(
cb.into_node_subscription_callback(),
AnySubscriptionCallback::Node(
NodeSubscriptionCallback::<TestMessage>::SyncLoanedWithMessageInfo(_)
),
));
assert!(matches!(
test_regular.into_node_subscription_callback(),
AnySubscriptionCallback::Node(NodeSubscriptionCallback::<TestMessage>::SyncRegular(_)),
));
assert!(matches!(
test_regular_with_info.into_node_subscription_callback(),
AnySubscriptionCallback::Node(
NodeSubscriptionCallback::<TestMessage>::SyncRegularWithMessageInfo(_)
),
));
assert!(matches!(
test_boxed.into_node_subscription_callback(),
AnySubscriptionCallback::Node(NodeSubscriptionCallback::<TestMessage>::SyncBoxed(_)),
));
assert!(matches!(
test_boxed_with_info.into_node_subscription_callback(),
AnySubscriptionCallback::Node(
NodeSubscriptionCallback::<TestMessage>::SyncBoxedWithMessageInfo(_)
),
));
assert!(matches!(
test_loaned.into_node_subscription_callback(),
AnySubscriptionCallback::Node(NodeSubscriptionCallback::<TestMessage>::SyncLoaned(_)),
));
assert!(matches!(
test_loaned_with_info.into_node_subscription_callback(),
AnySubscriptionCallback::Node(
NodeSubscriptionCallback::<TestMessage>::SyncLoanedWithMessageInfo(_)
),
));
}
fn test_regular(_msg: TestMessage) {}
fn test_regular_with_info(_msg: TestMessage, _info: MessageInfo) {}
fn test_boxed(_msg: Box<TestMessage>) {}
fn test_boxed_with_info(_msg: Box<TestMessage>, _info: MessageInfo) {}
fn test_loaned(_msg: ReadOnlyLoanedMessage<TestMessage>) {}
fn test_loaned_with_info(_msg: ReadOnlyLoanedMessage<TestMessage>, _info: MessageInfo) {}
}