Skip to content

Commit 238dadf

Browse files
Relax type of callback arguments to Router methods
1 parent 59e551e commit 238dadf

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

worker/src/router.rs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,11 @@ impl<'a, D: 'a> Router<'a, D> {
190190

191191
/// Register an HTTP handler that will exclusively respond to HEAD requests. Enables the use of
192192
/// `async/await` syntax in the callback.
193-
pub fn head_async<T>(mut self, pattern: &str, func: fn(Request, RouteContext<D>) -> T) -> Self
193+
pub fn head_async<T>(
194+
mut self,
195+
pattern: &str,
196+
func: impl Fn(Request, RouteContext<D>) -> T + 'a,
197+
) -> Self
194198
where
195199
T: Future<Output = Result<Response>> + 'a,
196200
{
@@ -204,7 +208,11 @@ impl<'a, D: 'a> Router<'a, D> {
204208

205209
/// Register an HTTP handler that will exclusively respond to GET requests. Enables the use of
206210
/// `async/await` syntax in the callback.
207-
pub fn get_async<T>(mut self, pattern: &str, func: fn(Request, RouteContext<D>) -> T) -> Self
211+
pub fn get_async<T>(
212+
mut self,
213+
pattern: &str,
214+
func: impl Fn(Request, RouteContext<D>) -> T + 'a,
215+
) -> Self
208216
where
209217
T: Future<Output = Result<Response>> + 'a,
210218
{
@@ -218,7 +226,11 @@ impl<'a, D: 'a> Router<'a, D> {
218226

219227
/// Register an HTTP handler that will exclusively respond to POST requests. Enables the use of
220228
/// `async/await` syntax in the callback.
221-
pub fn post_async<T>(mut self, pattern: &str, func: fn(Request, RouteContext<D>) -> T) -> Self
229+
pub fn post_async<T>(
230+
mut self,
231+
pattern: &str,
232+
func: impl Fn(Request, RouteContext<D>) -> T + 'a,
233+
) -> Self
222234
where
223235
T: Future<Output = Result<Response>> + 'a,
224236
{
@@ -232,7 +244,11 @@ impl<'a, D: 'a> Router<'a, D> {
232244

233245
/// Register an HTTP handler that will exclusively respond to PUT requests. Enables the use of
234246
/// `async/await` syntax in the callback.
235-
pub fn put_async<T>(mut self, pattern: &str, func: fn(Request, RouteContext<D>) -> T) -> Self
247+
pub fn put_async<T>(
248+
mut self,
249+
pattern: &str,
250+
func: impl Fn(Request, RouteContext<D>) -> T + 'a,
251+
) -> Self
236252
where
237253
T: Future<Output = Result<Response>> + 'a,
238254
{
@@ -246,7 +262,11 @@ impl<'a, D: 'a> Router<'a, D> {
246262

247263
/// Register an HTTP handler that will exclusively respond to PATCH requests. Enables the use of
248264
/// `async/await` syntax in the callback.
249-
pub fn patch_async<T>(mut self, pattern: &str, func: fn(Request, RouteContext<D>) -> T) -> Self
265+
pub fn patch_async<T>(
266+
mut self,
267+
pattern: &str,
268+
func: impl Fn(Request, RouteContext<D>) -> T + 'a,
269+
) -> Self
250270
where
251271
T: Future<Output = Result<Response>> + 'a,
252272
{
@@ -260,7 +280,11 @@ impl<'a, D: 'a> Router<'a, D> {
260280

261281
/// Register an HTTP handler that will exclusively respond to DELETE requests. Enables the use
262282
/// of `async/await` syntax in the callback.
263-
pub fn delete_async<T>(mut self, pattern: &str, func: fn(Request, RouteContext<D>) -> T) -> Self
283+
pub fn delete_async<T>(
284+
mut self,
285+
pattern: &str,
286+
func: impl Fn(Request, RouteContext<D>) -> T + 'a,
287+
) -> Self
264288
where
265289
T: Future<Output = Result<Response>> + 'a,
266290
{
@@ -277,7 +301,7 @@ impl<'a, D: 'a> Router<'a, D> {
277301
pub fn options_async<T>(
278302
mut self,
279303
pattern: &str,
280-
func: fn(Request, RouteContext<D>) -> T,
304+
func: impl Fn(Request, RouteContext<D>) -> T + 'a,
281305
) -> Self
282306
where
283307
T: Future<Output = Result<Response>> + 'a,
@@ -292,7 +316,11 @@ impl<'a, D: 'a> Router<'a, D> {
292316

293317
/// Register an HTTP handler that will respond to any requests. Enables the use of `async/await`
294318
/// syntax in the callback.
295-
pub fn on_async<T>(mut self, pattern: &str, func: fn(Request, RouteContext<D>) -> T) -> Self
319+
pub fn on_async<T>(
320+
mut self,
321+
pattern: &str,
322+
func: impl Fn(Request, RouteContext<D>) -> T + 'a,
323+
) -> Self
296324
where
297325
T: Future<Output = Result<Response>> + 'a,
298326
{
@@ -309,7 +337,7 @@ impl<'a, D: 'a> Router<'a, D> {
309337
pub fn or_else_any_method_async<T>(
310338
mut self,
311339
pattern: &str,
312-
func: fn(Request, RouteContext<D>) -> T,
340+
func: impl Fn(Request, RouteContext<D>) -> T + 'a,
313341
) -> Self
314342
where
315343
T: Future<Output = Result<Response>> + 'a,

0 commit comments

Comments
 (0)