r/rust•
barskern

Trouble moving clonable hyper::Service into async block

EDIT: Solved by u/fhsgoncalves solution.

This is not an issue when the warp Filter is Copy (playground), but when I try to add some non-copy state to the Filter I am unable to get it working. I've tried to manually clone the service at multiple points within the closure of the make_service_fn. When I had a similar issue before it worked to add a clone before the async move block (see example below), however that does not work in this case.

Could somebody help me get this to pass the borrowchecker, or is my thoughtprocess fundamentally flawed here?

Simplified problem

Actual problem (doesn't compile due to the playground not having access to warp)

Example of adding clone resolving this error

let tera = Tera::new(...);
let render_tera = move |WithTemplate { name, context }| {
	let tera2 = Arc::clone(&tera); // Adding this line fixed the issues here
	async move {
		tera2
			.render(
				&*name,
				&tera::Context::from_serialize(context).map_err(|e| warp::reject::custom(TemplateRender))?,
			)
			.map_err(|e| warp::reject::custom(TemplateRender))
	}
};

Posted Jan 25, 2020, 5:28 PM UTC · edited

113
fhsgoncalves•

I face the same problem yesterday, but after I cloned the service on every move, it compiled!

Try that: https://github.com/seanmonstar/warp/issues/16#issuecomment-578376676

2
barskernOP•

Thank you for the swift response, I will give that a shot!

EDIT: That works perfectly! Thank you again.

2
fhsgoncalves•

You're welcome!

1