Skip to content

Commit fb90f08

Browse files
authored
Add support for generic types to easy_wrapper (#54)
1 parent 6d2a097 commit fb90f08

File tree

2 files changed

+182
-4
lines changed

2 files changed

+182
-4
lines changed

strategy/src/lib.rs

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,75 @@ pub use pin_project_lite::pin_project;
130130
macro_rules! easy_wrapper {
131131
(
132132
$(#[$meta:meta])*
133-
$vis:vis struct $name:ident ($inner:ty => $output:ty);
133+
$vis:vis struct $name:ident
134+
135+
$(<
136+
$( $lifetime:lifetime $(: $lifetime_bound:lifetime)? ),* $(,)?
137+
$( $generics:ident
138+
$(: $generics_bound:path)?
139+
$(: ?$generics_unsized_bound:path)?
140+
$(: $generics_lifetime_bound:lifetime)?
141+
$(= $generics_default:ty)?
142+
),* $(,)?
143+
>)?
144+
145+
($inner:ty => $output:ty)
146+
147+
$(where
148+
$( $where_clause_ty:ty
149+
$(: $where_clause_bound:path)?
150+
$(: ?$where_clause_unsized_bound:path)?
151+
$(: $where_clause_lifetime_bound:lifetime)?
152+
),* $(,)?
153+
)?
154+
155+
;
156+
134157
$(#[$wait_meta:meta])*
135158
$wait_vis: vis wait();
136159
) => {
137160
$crate::pin_project! {
138161
$(#[$meta])*
139-
$vis struct $name {
162+
$vis struct $name $(<
163+
$( $lifetime $(: $lifetime_bound)? ),*
164+
$( $generics
165+
$(: $generics_bound)?
166+
$(: ?$generics_unsized_bound)?
167+
$(: $generics_lifetime_bound)?
168+
$(= $generics_default)?
169+
),*
170+
>)? $(
171+
where
172+
$( $where_clause_ty
173+
$(: $where_clause_bound)?
174+
$(: ?$where_clause_unsized_bound)?
175+
$(: $where_clause_lifetime_bound)?
176+
),*
177+
)? {
140178
#[pin]
141179
_inner: $crate::FutureWrapper<$inner>
142180
}
143181
}
144182

145-
impl $name {
183+
impl $(<
184+
$( $lifetime $(: $lifetime_bound)? ,)*
185+
$( $generics
186+
$(: $generics_bound)?
187+
$(: ?$generics_unsized_bound)?
188+
$(: $generics_lifetime_bound)?
189+
$(= $generics_default)?
190+
),*
191+
>)? $name $(<
192+
$( $lifetime ,)*
193+
$( $generics ),*
194+
>)? $(
195+
where
196+
$( $where_clause_ty
197+
$(: $where_clause_bound)?
198+
$(: ?$where_clause_unsized_bound)?
199+
$(: $where_clause_lifetime_bound)?
200+
),*
201+
)? {
146202
#[inline]
147203
fn _new(inner: $inner) -> Self {
148204
Self {
@@ -158,7 +214,27 @@ macro_rules! easy_wrapper {
158214
}
159215
}
160216

161-
impl ::core::future::Future for $name {
217+
impl $(<
218+
$( $lifetime $(: $lifetime_bound)? ,)*
219+
$( $generics
220+
$(: $generics_bound)?
221+
$(: ?$generics_unsized_bound)?
222+
$(: $generics_lifetime_bound)?
223+
$(= $generics_default)?
224+
),*
225+
>)? ::core::future::Future for $name $(
226+
<
227+
$( $lifetime ,)*
228+
$( $generics ),*
229+
>
230+
)? $(
231+
where
232+
$( $where_clause_ty
233+
$(: $where_clause_bound)?
234+
$(: ?$where_clause_unsized_bound)?
235+
$(: $where_clause_lifetime_bound)?
236+
),*
237+
)? {
162238
type Output = $output;
163239

164240
#[inline]

strategy/tests/easy_wrapper.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//! Testing of the `easy_wrapper!` macro.
2+
3+
use std::{marker::PhantomData, pin::Pin, task::Poll};
4+
5+
use event_listener_strategy::{easy_wrapper, EventListenerFuture, Strategy};
6+
7+
#[test]
8+
fn easy_wrapper_generics() {
9+
// Easy case.
10+
struct MyStrategy;
11+
12+
impl EventListenerFuture for MyStrategy {
13+
type Output = ();
14+
15+
fn poll_with_strategy<'a, S: Strategy<'a>>(
16+
self: Pin<&'a mut Self>,
17+
_strategy: &mut S,
18+
_context: &mut S::Context,
19+
) -> Poll<Self::Output> {
20+
Poll::Ready(())
21+
}
22+
}
23+
24+
easy_wrapper! {
25+
struct MyEasyWrapper(MyStrategy => ());
26+
wait();
27+
}
28+
29+
MyEasyWrapper::_new(MyStrategy).wait();
30+
31+
// Medium case with generics.
32+
struct MyStrategy2<T> {
33+
_marker: PhantomData<T>,
34+
}
35+
36+
impl<T> EventListenerFuture for MyStrategy2<T> {
37+
type Output = T;
38+
39+
fn poll_with_strategy<'a, S: Strategy<'a>>(
40+
self: Pin<&'a mut Self>,
41+
_strategy: &mut S,
42+
_context: &mut S::Context,
43+
) -> Poll<Self::Output> {
44+
unreachable!()
45+
}
46+
}
47+
48+
easy_wrapper! {
49+
struct MyEasyWrapper2<T>(MyStrategy2<T> => T);
50+
wait();
51+
}
52+
53+
// Medium mode with lifetime.
54+
struct MyStrategylt<'a> {
55+
_marker: PhantomData<&'a ()>,
56+
}
57+
58+
impl<'a> EventListenerFuture for MyStrategylt<'a> {
59+
type Output = &'a ();
60+
61+
fn poll_with_strategy<'b, S: Strategy<'b>>(
62+
self: Pin<&'b mut Self>,
63+
_strategy: &mut S,
64+
_context: &mut S::Context,
65+
) -> Poll<Self::Output> {
66+
unreachable!()
67+
}
68+
}
69+
70+
easy_wrapper! {
71+
struct MyEasyWrapperlt<'a>(MyStrategylt<'a> => &'a ());
72+
wait();
73+
}
74+
75+
// Hard mode with generic bounds.
76+
struct MyStrategy3<'a, T: ?Sized>
77+
where
78+
T: 'a,
79+
{
80+
_marker: PhantomData<&'a T>,
81+
}
82+
83+
impl<'a, T: ?Sized> EventListenerFuture for MyStrategy3<'a, T>
84+
where
85+
T: 'a,
86+
{
87+
type Output = &'a T;
88+
89+
fn poll_with_strategy<'b, S: Strategy<'b>>(
90+
self: Pin<&'b mut Self>,
91+
_strategy: &mut S,
92+
_context: &mut S::Context,
93+
) -> Poll<Self::Output> {
94+
unreachable!()
95+
}
96+
}
97+
98+
easy_wrapper! {
99+
struct MyEasyWrapper3<'a, T: ?Sized>(MyStrategy3<'a, T> => &'a T) where T: 'a;
100+
wait();
101+
}
102+
}

0 commit comments

Comments
 (0)