Skip to content

Instantly share code, notes, and snippets.

@blankhart
Last active July 3, 2020 12:41
Show Gist options
  • Save blankhart/9a254cd89ad4a9718125137ae3bfa39f to your computer and use it in GitHub Desktop.
Save blankhart/9a254cd89ad4a9718125137ae3bfa39f to your computer and use it in GitHub Desktop.
actix-web scope wrapper
// These type signatures do not quite work.
fn redirect_if_unauthorized<T, R>(
req: actix_web::dev::ServiceRequest,
srv: &mut T::Service,
) -> impl future::Future<Output = Result<actix_web::dev::ServiceResponse, Error>> + Clone
where
T: actix_service::ServiceFactory<
Config = (),
Request = actix_web::dev::ServiceRequest,
Response = actix_web::dev::ServiceResponse,
Error = Error,
InitError = (),
>,
{
match req.get_identity() {
Some(id) => {
trace!("Found identity cookie {}", id);
use actix_service::Service;
srv.call(req)
}
None => future::Either::Right(future::ok(dev::ServiceResponse::new(
req.into_parts().0,
goto("/login"),
))),
}
}
/*
Error:
`match` arms have incompatible types
expected associated type, found enum `futures_util::future::either::Either`
note: expected type `<<T as actix_service::ServiceFactory>::Service as actix_service::Service>::Future`
found enum `futures_util::future::either::Either<_, futures_util::future::ready::Ready<std::result::Result<actix_web::service::ServiceResponse, _>>>`rustc(E0308)
lib.rs(109, 5): consider calling `actix_service::Service::call`
main.rs(331, 3): `match` arms have incompatible types
main.rs(335, 7): this is found to be of type `<<T as actix_service::ServiceFactory>::Service as actix_service::Service>::Future`
main.rs(337, 13): expected associated type, found enum `futures_util::future::either::Either`
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment