Skip to content

Commit d723301

Browse files
authored
remove Send and Sync bounds from Rust SDK router (#2198)
These bounds were preventing users from calling async functions which use `http::executor` since it uses `Rc<RefCell<_>>` internally, which is neither `Send` nor `Sync`. Signed-off-by: Joel Dice <[email protected]>
1 parent b435f83 commit d723301

File tree

1 file changed

+87
-87
lines changed

1 file changed

+87
-87
lines changed

sdk/rust/src/http/router.rs

Lines changed: 87 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ use std::{collections::HashMap, fmt::Display};
1111
///
1212
/// This trait is automatically implemented for `Fn` types, and so is rarely implemented
1313
/// directly by Spin users.
14-
#[async_trait]
15-
pub trait Handler: Send + Sync {
14+
#[async_trait(?Send)]
15+
pub trait Handler {
1616
/// Invoke the handler.
1717
async fn handle(&self, req: Request, params: Params) -> Response;
1818
}
1919

20-
#[async_trait]
20+
#[async_trait(?Send)]
2121
impl Handler for Box<dyn Handler> {
2222
async fn handle(&self, req: Request, params: Params) -> Response {
2323
self.as_ref().handle(req, params).await
2424
}
2525
}
2626

27-
#[async_trait]
27+
#[async_trait(?Send)]
2828
impl<F, Fut> Handler for F
2929
where
30-
F: Fn(Request, Params) -> Fut + Send + Sync + 'static,
31-
Fut: Future<Output = Response> + Send + 'static,
30+
F: Fn(Request, Params) -> Fut + 'static,
31+
Fut: Future<Output = Response> + 'static,
3232
{
3333
async fn handle(&self, req: Request, params: Params) -> Response {
3434
let fut = (self)(req, params);
@@ -154,10 +154,10 @@ impl Router {
154154
/// Register a handler at the path for all methods.
155155
pub fn any<F, Req, Resp>(&mut self, path: &str, handler: F)
156156
where
157-
F: Fn(Req, Params) -> Resp + 'static + Send + Sync,
158-
Req: TryFromRequest + 'static + Send,
159-
Req::Error: IntoResponse + Send + 'static,
160-
Resp: IntoResponse + 'static + Send,
157+
F: Fn(Req, Params) -> Resp + 'static,
158+
Req: TryFromRequest + 'static,
159+
Req::Error: IntoResponse + 'static,
160+
Resp: IntoResponse + 'static,
161161
{
162162
let handler = move |req, params| {
163163
let res = TryFromRequest::try_from_request(req).map(|r| handler(r, params));
@@ -175,11 +175,11 @@ impl Router {
175175
/// Register an async handler at the path for all methods.
176176
pub fn any_async<F, Fut, I, O>(&mut self, path: &str, handler: F)
177177
where
178-
F: Fn(I, Params) -> Fut + 'static + Send + Sync,
179-
Fut: Future<Output = O> + Send + 'static,
180-
I: TryFromRequest + 'static + Send,
181-
I::Error: IntoResponse + Send + 'static,
182-
O: IntoResponse + 'static + Send,
178+
F: Fn(I, Params) -> Fut + 'static,
179+
Fut: Future<Output = O> + 'static,
180+
I: TryFromRequest + 'static,
181+
I::Error: IntoResponse + 'static,
182+
O: IntoResponse + 'static,
183183
{
184184
let handler = move |req, params| {
185185
let res = TryFromRequest::try_from_request(req).map(|r| handler(r, params));
@@ -197,10 +197,10 @@ impl Router {
197197
/// Register a handler at the path for the specified HTTP method.
198198
pub fn add<F, Req, Resp>(&mut self, path: &str, method: Method, handler: F)
199199
where
200-
F: Fn(Req, Params) -> Resp + 'static + Send + Sync,
201-
Req: TryFromRequest + 'static + Send,
202-
Req::Error: IntoResponse + Send + 'static,
203-
Resp: IntoResponse + 'static + Send,
200+
F: Fn(Req, Params) -> Resp + 'static,
201+
Req: TryFromRequest + 'static,
202+
Req::Error: IntoResponse + 'static,
203+
Resp: IntoResponse + 'static,
204204
{
205205
let handler = move |req, params| {
206206
let res = TryFromRequest::try_from_request(req).map(|r| handler(r, params));
@@ -218,11 +218,11 @@ impl Router {
218218
/// Register an async handler at the path for the specified HTTP method.
219219
pub fn add_async<F, Fut, I, O>(&mut self, path: &str, method: Method, handler: F)
220220
where
221-
F: Fn(I, Params) -> Fut + 'static + Send + Sync,
222-
Fut: Future<Output = O> + Send + 'static,
223-
I: TryFromRequest + 'static + Send,
224-
I::Error: IntoResponse + Send + 'static,
225-
O: IntoResponse + 'static + Send,
221+
F: Fn(I, Params) -> Fut + 'static,
222+
Fut: Future<Output = O> + 'static,
223+
I: TryFromRequest + 'static,
224+
I::Error: IntoResponse + 'static,
225+
O: IntoResponse + 'static,
226226
{
227227
let handler = move |req, params| {
228228
let res = TryFromRequest::try_from_request(req).map(|r| handler(r, params));
@@ -244,160 +244,160 @@ impl Router {
244244
/// Register a handler at the path for the HTTP GET method.
245245
pub fn get<F, Req, Resp>(&mut self, path: &str, handler: F)
246246
where
247-
F: Fn(Req, Params) -> Resp + 'static + Send + Sync,
248-
Req: TryFromRequest + 'static + Send,
249-
Req::Error: IntoResponse + Send + 'static,
250-
Resp: IntoResponse + 'static + Send,
247+
F: Fn(Req, Params) -> Resp + 'static,
248+
Req: TryFromRequest + 'static,
249+
Req::Error: IntoResponse + 'static,
250+
Resp: IntoResponse + 'static,
251251
{
252252
self.add(path, Method::Get, handler)
253253
}
254254

255255
/// Register an async handler at the path for the HTTP GET method.
256256
pub fn get_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
257257
where
258-
F: Fn(Req, Params) -> Fut + 'static + Send + Sync,
259-
Fut: Future<Output = Resp> + Send + 'static,
260-
Req: TryFromRequest + 'static + Send,
261-
Req::Error: IntoResponse + Send + 'static,
262-
Resp: IntoResponse + 'static + Send,
258+
F: Fn(Req, Params) -> Fut + 'static,
259+
Fut: Future<Output = Resp> + 'static,
260+
Req: TryFromRequest + 'static,
261+
Req::Error: IntoResponse + 'static,
262+
Resp: IntoResponse + 'static,
263263
{
264264
self.add_async(path, Method::Get, handler)
265265
}
266266

267267
/// Register a handler at the path for the HTTP HEAD method.
268268
pub fn head<F, Req, Resp>(&mut self, path: &str, handler: F)
269269
where
270-
F: Fn(Req, Params) -> Resp + 'static + Send + Sync,
271-
Req: TryFromRequest + 'static + Send,
272-
Req::Error: IntoResponse + Send + 'static,
273-
Resp: IntoResponse + 'static + Send,
270+
F: Fn(Req, Params) -> Resp + 'static,
271+
Req: TryFromRequest + 'static,
272+
Req::Error: IntoResponse + 'static,
273+
Resp: IntoResponse + 'static,
274274
{
275275
self.add(path, Method::Head, handler)
276276
}
277277

278278
/// Register an async handler at the path for the HTTP HEAD method.
279279
pub fn head_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
280280
where
281-
F: Fn(Req, Params) -> Fut + 'static + Send + Sync,
282-
Fut: Future<Output = Resp> + Send + 'static,
283-
Req: TryFromRequest + 'static + Send,
284-
Req::Error: IntoResponse + Send + 'static,
285-
Resp: IntoResponse + 'static + Send,
281+
F: Fn(Req, Params) -> Fut + 'static,
282+
Fut: Future<Output = Resp> + 'static,
283+
Req: TryFromRequest + 'static,
284+
Req::Error: IntoResponse + 'static,
285+
Resp: IntoResponse + 'static,
286286
{
287287
self.add_async(path, Method::Head, handler)
288288
}
289289

290290
/// Register a handler at the path for the HTTP POST method.
291291
pub fn post<F, Req, Resp>(&mut self, path: &str, handler: F)
292292
where
293-
F: Fn(Req, Params) -> Resp + 'static + Send + Sync,
294-
Req: TryFromRequest + 'static + Send,
295-
Req::Error: IntoResponse + Send + 'static,
296-
Resp: IntoResponse + 'static + Send,
293+
F: Fn(Req, Params) -> Resp + 'static,
294+
Req: TryFromRequest + 'static,
295+
Req::Error: IntoResponse + 'static,
296+
Resp: IntoResponse + 'static,
297297
{
298298
self.add(path, Method::Post, handler)
299299
}
300300

301301
/// Register an async handler at the path for the HTTP POST method.
302302
pub fn post_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
303303
where
304-
F: Fn(Req, Params) -> Fut + 'static + Send + Sync,
305-
Fut: Future<Output = Resp> + Send + 'static,
306-
Req: TryFromRequest + 'static + Send,
307-
Req::Error: IntoResponse + Send + 'static,
308-
Resp: IntoResponse + 'static + Send,
304+
F: Fn(Req, Params) -> Fut + 'static,
305+
Fut: Future<Output = Resp> + 'static,
306+
Req: TryFromRequest + 'static,
307+
Req::Error: IntoResponse + 'static,
308+
Resp: IntoResponse + 'static,
309309
{
310310
self.add_async(path, Method::Post, handler)
311311
}
312312

313313
/// Register a handler at the path for the HTTP DELETE method.
314314
pub fn delete<F, Req, Resp>(&mut self, path: &str, handler: F)
315315
where
316-
F: Fn(Req, Params) -> Resp + 'static + Send + Sync,
317-
Req: TryFromRequest + 'static + Send,
318-
Req::Error: IntoResponse + Send + 'static,
319-
Resp: IntoResponse + 'static + Send,
316+
F: Fn(Req, Params) -> Resp + 'static,
317+
Req: TryFromRequest + 'static,
318+
Req::Error: IntoResponse + 'static,
319+
Resp: IntoResponse + 'static,
320320
{
321321
self.add(path, Method::Delete, handler)
322322
}
323323

324324
/// Register an async handler at the path for the HTTP DELETE method.
325325
pub fn delete_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
326326
where
327-
F: Fn(Req, Params) -> Fut + 'static + Send + Sync,
328-
Fut: Future<Output = Resp> + Send + 'static,
329-
Req: TryFromRequest + 'static + Send,
330-
Req::Error: IntoResponse + Send + 'static,
331-
Resp: IntoResponse + 'static + Send,
327+
F: Fn(Req, Params) -> Fut + 'static,
328+
Fut: Future<Output = Resp> + 'static,
329+
Req: TryFromRequest + 'static,
330+
Req::Error: IntoResponse + 'static,
331+
Resp: IntoResponse + 'static,
332332
{
333333
self.add_async(path, Method::Delete, handler)
334334
}
335335

336336
/// Register a handler at the path for the HTTP PUT method.
337337
pub fn put<F, Req, Resp>(&mut self, path: &str, handler: F)
338338
where
339-
F: Fn(Req, Params) -> Resp + 'static + Send + Sync,
340-
Req: TryFromRequest + 'static + Send,
341-
Req::Error: IntoResponse + Send + 'static,
342-
Resp: IntoResponse + 'static + Send,
339+
F: Fn(Req, Params) -> Resp + 'static,
340+
Req: TryFromRequest + 'static,
341+
Req::Error: IntoResponse + 'static,
342+
Resp: IntoResponse + 'static,
343343
{
344344
self.add(path, Method::Put, handler)
345345
}
346346

347347
/// Register an async handler at the path for the HTTP PUT method.
348348
pub fn put_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
349349
where
350-
F: Fn(Req, Params) -> Fut + 'static + Send + Sync,
351-
Fut: Future<Output = Resp> + Send + 'static,
352-
Req: TryFromRequest + 'static + Send,
353-
Req::Error: IntoResponse + Send + 'static,
354-
Resp: IntoResponse + 'static + Send,
350+
F: Fn(Req, Params) -> Fut + 'static,
351+
Fut: Future<Output = Resp> + 'static,
352+
Req: TryFromRequest + 'static,
353+
Req::Error: IntoResponse + 'static,
354+
Resp: IntoResponse + 'static,
355355
{
356356
self.add_async(path, Method::Put, handler)
357357
}
358358

359359
/// Register a handler at the path for the HTTP PATCH method.
360360
pub fn patch<F, Req, Resp>(&mut self, path: &str, handler: F)
361361
where
362-
F: Fn(Req, Params) -> Resp + 'static + Send + Sync,
363-
Req: TryFromRequest + 'static + Send,
364-
Req::Error: IntoResponse + Send + 'static,
365-
Resp: IntoResponse + 'static + Send,
362+
F: Fn(Req, Params) -> Resp + 'static,
363+
Req: TryFromRequest + 'static,
364+
Req::Error: IntoResponse + 'static,
365+
Resp: IntoResponse + 'static,
366366
{
367367
self.add(path, Method::Patch, handler)
368368
}
369369

370370
/// Register an async handler at the path for the HTTP PATCH method.
371371
pub fn patch_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
372372
where
373-
F: Fn(Req, Params) -> Fut + 'static + Send + Sync,
374-
Fut: Future<Output = Resp> + Send + 'static,
375-
Req: TryFromRequest + 'static + Send,
376-
Req::Error: IntoResponse + Send + 'static,
377-
Resp: IntoResponse + 'static + Send,
373+
F: Fn(Req, Params) -> Fut + 'static,
374+
Fut: Future<Output = Resp> + 'static,
375+
Req: TryFromRequest + 'static,
376+
Req::Error: IntoResponse + 'static,
377+
Resp: IntoResponse + 'static,
378378
{
379379
self.add_async(path, Method::Patch, handler)
380380
}
381381

382382
/// Register a handler at the path for the HTTP OPTIONS method.
383383
pub fn options<F, Req, Resp>(&mut self, path: &str, handler: F)
384384
where
385-
F: Fn(Req, Params) -> Resp + 'static + Send + Sync,
386-
Req: TryFromRequest + 'static + Send,
387-
Req::Error: IntoResponse + Send + 'static,
388-
Resp: IntoResponse + 'static + Send,
385+
F: Fn(Req, Params) -> Resp + 'static,
386+
Req: TryFromRequest + 'static,
387+
Req::Error: IntoResponse + 'static,
388+
Resp: IntoResponse + 'static,
389389
{
390390
self.add(path, Method::Options, handler)
391391
}
392392

393393
/// Register an async handler at the path for the HTTP OPTIONS method.
394394
pub fn options_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
395395
where
396-
F: Fn(Req, Params) -> Fut + 'static + Send + Sync,
397-
Fut: Future<Output = Resp> + Send + 'static,
398-
Req: TryFromRequest + 'static + Send,
399-
Req::Error: IntoResponse + Send + 'static,
400-
Resp: IntoResponse + 'static + Send,
396+
F: Fn(Req, Params) -> Fut + 'static,
397+
Fut: Future<Output = Resp> + 'static,
398+
Req: TryFromRequest + 'static,
399+
Req::Error: IntoResponse + 'static,
400+
Resp: IntoResponse + 'static,
401401
{
402402
self.add_async(path, Method::Options, handler)
403403
}

0 commit comments

Comments
 (0)