Skip to content

Commit 49379f1

Browse files
committed
Simplify implementation of subscription deduction
Signed-off-by: Michael X. Grey <[email protected]>
1 parent f8a5803 commit 49379f1

File tree

2 files changed

+36
-102
lines changed

2 files changed

+36
-102
lines changed

rclrs/src/subscription/into_async_subscription_callback.rs

Lines changed: 18 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,112 +20,76 @@ where
2020
fn into_async_subscription_callback(self) -> AnySubscriptionCallback<T, ()>;
2121
}
2222

23-
// We need one implementation per arity. This was inspired by Bevy's systems.
24-
impl<T, A0, Out, Func> IntoAsyncSubscriptionCallback<T, (A0,)> for Func
25-
where
26-
T: Message,
27-
(A0,): SubscriptionAsyncArgs<T, Func>,
28-
Out: Future<Output = ()> + Send + 'static,
29-
Func: FnMut(A0) -> Out + Send + 'static,
30-
{
31-
fn into_async_subscription_callback(self) -> AnySubscriptionCallback<T, ()> {
32-
<(A0,) as SubscriptionAsyncArgs<T, Func>>::into_any_callback(self)
33-
}
34-
}
35-
36-
impl<T, A0, A1, Out, Func> IntoAsyncSubscriptionCallback<T, (A0, A1)> for Func
37-
where
38-
T: Message,
39-
(A0, A1): SubscriptionAsyncArgs<T, Func>,
40-
Out: Future<Output = ()> + Send + 'static,
41-
Func: FnMut(A0, A1) -> Out + Send + 'static,
42-
{
43-
fn into_async_subscription_callback(self) -> AnySubscriptionCallback<T, ()> {
44-
<(A0, A1) as SubscriptionAsyncArgs<T, Func>>::into_any_callback(self)
45-
}
46-
}
47-
48-
/// Helper trait for SubscriptionCallback.
49-
///
50-
/// For each tuple of args, it provides conversion from a function with
51-
/// these args to the correct enum variant.
52-
trait SubscriptionAsyncArgs<T, Func>
53-
where
54-
T: Message,
55-
{
56-
fn into_any_callback(func: Func) -> AnySubscriptionCallback<T, ()>;
57-
}
58-
59-
impl<T, Out, Func> SubscriptionAsyncArgs<T, Func> for (T,)
23+
impl<T, Out, Func> IntoAsyncSubscriptionCallback<T, (T,)> for Func
6024
where
6125
T: Message,
6226
Func: FnMut(T) -> Out + Send + 'static,
6327
Out: Future<Output = ()> + Send + 'static,
6428
{
65-
fn into_any_callback(mut func: Func) -> AnySubscriptionCallback<T, ()> {
66-
NodeSubscriptionCallback::Regular(Box::new(move |message| Box::pin(func(message)))).into()
29+
fn into_async_subscription_callback(mut self) -> AnySubscriptionCallback<T, ()> {
30+
NodeSubscriptionCallback::Regular(Box::new(move |message| Box::pin(self(message)))).into()
6731
}
6832
}
6933

70-
impl<T, Out, Func> SubscriptionAsyncArgs<T, Func> for (T, MessageInfo)
34+
impl<T, Out, Func> IntoAsyncSubscriptionCallback<T, (T, MessageInfo)> for Func
7135
where
7236
T: Message,
7337
Func: FnMut(T, MessageInfo) -> Out + Send + 'static,
7438
Out: Future<Output = ()> + Send + 'static,
7539
{
76-
fn into_any_callback(mut func: Func) -> AnySubscriptionCallback<T, ()> {
40+
fn into_async_subscription_callback(mut self) -> AnySubscriptionCallback<T, ()> {
7741
NodeSubscriptionCallback::RegularWithMessageInfo(Box::new(move |message, info| {
78-
Box::pin(func(message, info))
42+
Box::pin(self(message, info))
7943
}))
8044
.into()
8145
}
8246
}
8347

84-
impl<T, Out, Func> SubscriptionAsyncArgs<T, Func> for (Box<T>,)
48+
impl<T, Out, Func> IntoAsyncSubscriptionCallback<T, (Box<T>,)> for Func
8549
where
8650
T: Message,
8751
Func: FnMut(Box<T>) -> Out + Send + 'static,
8852
Out: Future<Output = ()> + Send + 'static,
8953
{
90-
fn into_any_callback(mut func: Func) -> AnySubscriptionCallback<T, ()> {
91-
NodeSubscriptionCallback::Boxed(Box::new(move |message| Box::pin(func(message)))).into()
54+
fn into_async_subscription_callback(mut self) -> AnySubscriptionCallback<T, ()> {
55+
NodeSubscriptionCallback::Boxed(Box::new(move |message| Box::pin(self(message)))).into()
9256
}
9357
}
9458

95-
impl<T, F, Func> SubscriptionAsyncArgs<T, Func> for (Box<T>, MessageInfo)
59+
impl<T, F, Func> IntoAsyncSubscriptionCallback<T, (Box<T>, MessageInfo)> for Func
9660
where
9761
T: Message,
9862
Func: FnMut(Box<T>, MessageInfo) -> F + Send + 'static,
9963
F: Future<Output = ()> + Send + 'static,
10064
{
101-
fn into_any_callback(mut func: Func) -> AnySubscriptionCallback<T, ()> {
65+
fn into_async_subscription_callback(mut self) -> AnySubscriptionCallback<T, ()> {
10266
NodeSubscriptionCallback::BoxedWithMessageInfo(Box::new(move |message, info| {
103-
Box::pin(func(message, info))
67+
Box::pin(self(message, info))
10468
}))
10569
.into()
10670
}
10771
}
10872

109-
impl<T, F, Func> SubscriptionAsyncArgs<T, Func> for (ReadOnlyLoanedMessage<T>,)
73+
impl<T, F, Func> IntoAsyncSubscriptionCallback<T, (ReadOnlyLoanedMessage<T>,)> for Func
11074
where
11175
T: Message,
11276
Func: FnMut(ReadOnlyLoanedMessage<T>) -> F + Send + 'static,
11377
F: Future<Output = ()> + Send + 'static,
11478
{
115-
fn into_any_callback(mut func: Func) -> AnySubscriptionCallback<T, ()> {
116-
NodeSubscriptionCallback::Loaned(Box::new(move |message| Box::pin(func(message)))).into()
79+
fn into_async_subscription_callback(mut self) -> AnySubscriptionCallback<T, ()> {
80+
NodeSubscriptionCallback::Loaned(Box::new(move |message| Box::pin(self(message)))).into()
11781
}
11882
}
11983

120-
impl<T, F, Func> SubscriptionAsyncArgs<T, Func> for (ReadOnlyLoanedMessage<T>, MessageInfo)
84+
impl<T, F, Func> IntoAsyncSubscriptionCallback<T, (ReadOnlyLoanedMessage<T>, MessageInfo)> for Func
12185
where
12286
T: Message,
12387
Func: FnMut(ReadOnlyLoanedMessage<T>, MessageInfo) -> F + Send + 'static,
12488
F: Future<Output = ()> + Send + 'static,
12589
{
126-
fn into_any_callback(mut func: Func) -> AnySubscriptionCallback<T, ()> {
90+
fn into_async_subscription_callback(mut self) -> AnySubscriptionCallback<T, ()> {
12791
NodeSubscriptionCallback::LoanedWithMessageInfo(Box::new(move |message, info| {
128-
Box::pin(func(message, info))
92+
Box::pin(self(message, info))
12993
}))
13094
.into()
13195
}

rclrs/src/subscription/into_node_subscription_callback.rs

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,13 @@ where
1919
fn into_node_subscription_callback(self) -> AnySubscriptionCallback<T, ()>;
2020
}
2121

22-
// We need one implementation per arity. This was inspired by Bevy's systems.
23-
impl<T, A0, Func> IntoNodeSubscriptionCallback<T, (A0,)> for Func
24-
where
25-
T: Message,
26-
(A0,): NodeSubscriptionArgs<T, Func>,
27-
Func: Fn(A0) + Send + Sync + 'static,
28-
{
29-
fn into_node_subscription_callback(self) -> AnySubscriptionCallback<T, ()> {
30-
<(A0,) as NodeSubscriptionArgs<T, Func>>::into_any_callback(self)
31-
}
32-
}
33-
34-
impl<T, A0, A1, Func> IntoNodeSubscriptionCallback<T, (A0, A1)> for Func
35-
where
36-
T: Message,
37-
(A0, A1): NodeSubscriptionArgs<T, Func>,
38-
Func: Fn(A0, A1) + Clone + Send + 'static,
39-
{
40-
fn into_node_subscription_callback(self) -> AnySubscriptionCallback<T, ()> {
41-
<(A0, A1) as NodeSubscriptionArgs<T, Func>>::into_any_callback(self)
42-
}
43-
}
44-
45-
trait NodeSubscriptionArgs<T, Func>
46-
where
47-
T: Message,
48-
{
49-
fn into_any_callback(func: Func) -> AnySubscriptionCallback<T, ()>;
50-
}
51-
52-
impl<T, Func> NodeSubscriptionArgs<T, Func> for (T,)
22+
impl<T, Func> IntoNodeSubscriptionCallback<T, (T,)> for Func
5323
where
5424
T: Message,
5525
Func: Fn(T) + Send + Sync + 'static,
5626
{
57-
fn into_any_callback(func: Func) -> AnySubscriptionCallback<T, ()> {
58-
let func = Arc::new(func);
27+
fn into_node_subscription_callback(self) -> AnySubscriptionCallback<T, ()> {
28+
let func = Arc::new(self);
5929
NodeSubscriptionCallback::Regular(Box::new(move |message| {
6030
let f = Arc::clone(&func);
6131
Box::pin(async move {
@@ -66,13 +36,13 @@ where
6636
}
6737
}
6838

69-
impl<T, Func> NodeSubscriptionArgs<T, Func> for (T, MessageInfo)
39+
impl<T, Func> IntoNodeSubscriptionCallback<T, (T, MessageInfo)> for Func
7040
where
7141
T: Message,
7242
Func: Fn(T, MessageInfo) + Send + Sync + 'static,
7343
{
74-
fn into_any_callback(func: Func) -> AnySubscriptionCallback<T, ()> {
75-
let func = Arc::new(func);
44+
fn into_node_subscription_callback(self) -> AnySubscriptionCallback<T, ()> {
45+
let func = Arc::new(self);
7646
NodeSubscriptionCallback::RegularWithMessageInfo(Box::new(move |message, info| {
7747
let f = Arc::clone(&func);
7848
Box::pin(async move {
@@ -83,13 +53,13 @@ where
8353
}
8454
}
8555

86-
impl<T, Func> NodeSubscriptionArgs<T, Func> for (Box<T>,)
56+
impl<T, Func> IntoNodeSubscriptionCallback<T, (Box<T>,)> for Func
8757
where
8858
T: Message,
8959
Func: Fn(Box<T>) + Send + Sync + 'static,
9060
{
91-
fn into_any_callback(func: Func) -> AnySubscriptionCallback<T, ()> {
92-
let func = Arc::new(func);
61+
fn into_node_subscription_callback(self) -> AnySubscriptionCallback<T, ()> {
62+
let func = Arc::new(self);
9363
NodeSubscriptionCallback::Boxed(Box::new(move |message| {
9464
let f = Arc::clone(&func);
9565
Box::pin(async move {
@@ -100,13 +70,13 @@ where
10070
}
10171
}
10272

103-
impl<T, Func> NodeSubscriptionArgs<T, Func> for (Box<T>, MessageInfo)
73+
impl<T, Func> IntoNodeSubscriptionCallback<T, (Box<T>, MessageInfo)> for Func
10474
where
10575
T: Message,
10676
Func: Fn(Box<T>, MessageInfo) + Send + Sync + 'static,
10777
{
108-
fn into_any_callback(func: Func) -> AnySubscriptionCallback<T, ()> {
109-
let func = Arc::new(func);
78+
fn into_node_subscription_callback(self) -> AnySubscriptionCallback<T, ()> {
79+
let func = Arc::new(self);
11080
NodeSubscriptionCallback::BoxedWithMessageInfo(Box::new(move |message, info| {
11181
let f = Arc::clone(&func);
11282
Box::pin(async move {
@@ -117,13 +87,13 @@ where
11787
}
11888
}
11989

120-
impl<T, Func> NodeSubscriptionArgs<T, Func> for (ReadOnlyLoanedMessage<T>,)
90+
impl<T, Func> IntoNodeSubscriptionCallback<T, (ReadOnlyLoanedMessage<T>,)> for Func
12191
where
12292
T: Message,
12393
Func: Fn(ReadOnlyLoanedMessage<T>) + Send + Sync + 'static,
12494
{
125-
fn into_any_callback(func: Func) -> AnySubscriptionCallback<T, ()> {
126-
let func = Arc::new(func);
95+
fn into_node_subscription_callback(self) -> AnySubscriptionCallback<T, ()> {
96+
let func = Arc::new(self);
12797
NodeSubscriptionCallback::Loaned(Box::new(move |message| {
12898
let f = Arc::clone(&func);
12999
Box::pin(async move {
@@ -134,13 +104,13 @@ where
134104
}
135105
}
136106

137-
impl<T, Func> NodeSubscriptionArgs<T, Func> for (ReadOnlyLoanedMessage<T>, MessageInfo)
107+
impl<T, Func> IntoNodeSubscriptionCallback<T, (ReadOnlyLoanedMessage<T>, MessageInfo)> for Func
138108
where
139109
T: Message,
140110
Func: Fn(ReadOnlyLoanedMessage<T>, MessageInfo) + Send + Sync + 'static,
141111
{
142-
fn into_any_callback(func: Func) -> AnySubscriptionCallback<T, ()> {
143-
let func = Arc::new(func);
112+
fn into_node_subscription_callback(self) -> AnySubscriptionCallback<T, ()> {
113+
let func = Arc::new(self);
144114
NodeSubscriptionCallback::LoanedWithMessageInfo(Box::new(move |message, info| {
145115
let f = Arc::clone(&func);
146116
Box::pin(async move {

0 commit comments

Comments
 (0)