seanmonstar / warp

A super-easy, composable, web server framework for warp speeds.
https://seanmonstar.com/post/176530511587/warp
MIT License
9.58k stars 720 forks source link

Can't use warp with an Rc (!Send) state (single-threaded executor) #1022

Open SylvainGarrigues opened 1 year ago

SylvainGarrigues commented 1 year ago

I'm am running warp as a service with a single-threaded hyper server.

The setup is based on: https://github.com/hyperium/hyper/blob/0.14.x/examples/single_threaded.rs

which allows to use a state shared through an Rc pointer.

I am trying to port the warp todos example in this setup, with an Rc/Refcell because Arc is not needed, and I cannot because this line: https://github.com/seanmonstar/warp/blob/4e9c4fd6ce238197fd1088061bbc07fa2852cb0f/examples/todos.rs#L100

creates a Map filter which does not implement warp::filter::FilterBase, because warp's implementation requires to use Send closures:

impl<T, F> FilterBase for Map<T, F>
where
    T: Filter,
    F: Func<T::Extract> + Clone + Send,

(see the Send trait on the last line - taken from https://github.com/seanmonstar/warp/blob/master/src/filter/map.rs).

It is so frustrating to use sync primitives (Arc / Mutex) when they are not needed!

Is there any workaround? Is the Send trait necessary there?

seanmonstar commented 1 year ago

I don't remember if the Send trait is needed. I assume it was added to make something else happy, if you wanted to try to remove it and see if the library still compiles...

SylvainGarrigues commented 1 year ago

I feel like it was introduced at that time: https://github.com/seanmonstar/warp/commit/79ead366582e1df67208c59dadf1a6a2e1475eee#diff-ff6d351d5b8e9ccd8131a2368e902858310c79aa975f2ed11fd53c6b7da68817

Also the AndThen filter has the same Send requirement so I guess relaxing the Send trait only for Map won't help much :(

https://github.com/seanmonstar/warp/blob/4e9c4fd6ce238197fd1088061bbc07fa2852cb0f/src/filter/and_then.rs#L20

SylvainGarrigues commented 1 year ago

I don't remember if the Send trait is needed. I assume it was added to make something else happy, if you wanted to try to remove it and see if the library still compiles...

I had to remove + Send all around the place and even disable some code (e.g. warp server stuff) to make it compile without Send, so I am enclines to say there is no easy way to use warp with a single-threaded executor and a !Send state.