boxed.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. use crate::service::{Service, ServiceFactory};
  2. use futures_core::future::{BoxFuture};
  3. pub fn factory<SF, Req>(factory: SF) -> BoxServiceFactory<SF::Context, Req, SF::Response, SF::Error>
  4. where
  5. SF: ServiceFactory<Req> + 'static + Sync + Send,
  6. Req: 'static,
  7. SF::Response: 'static,
  8. SF::Service: 'static,
  9. SF::Future: 'static,
  10. SF::Error: 'static + Send + Sync,
  11. <SF as ServiceFactory<Req>>::Service: Sync + Send,
  12. <<SF as ServiceFactory<Req>>::Service as Service<Req>>::Future: Send + Sync,
  13. <SF as ServiceFactory<Req>>::Future: Send + Sync,
  14. {
  15. BoxServiceFactory(Box::new(FactoryWrapper(factory)))
  16. }
  17. type Inner<Cfg, Req, Res, Err> = Box<
  18. dyn ServiceFactory<
  19. Req,
  20. Context = Cfg,
  21. Response = Res,
  22. Error = Err,
  23. Service = BoxService<Req, Res, Err>,
  24. Future = BoxFuture<'static, Result<BoxService<Req, Res, Err>, Err>>,
  25. > + Sync
  26. + Send,
  27. >;
  28. pub struct BoxServiceFactory<Cfg, Req, Res, Err>(Inner<Cfg, Req, Res, Err>);
  29. impl<Cfg, Req, Res, Err> ServiceFactory<Req> for BoxServiceFactory<Cfg, Req, Res, Err>
  30. where
  31. Req: 'static,
  32. Res: 'static,
  33. Err: 'static,
  34. {
  35. type Response = Res;
  36. type Error = Err;
  37. type Service = BoxService<Req, Res, Err>;
  38. type Context = Cfg;
  39. type Future = BoxFuture<'static, Result<Self::Service, Self::Error>>;
  40. fn new_service(&self, cfg: Cfg) -> Self::Future { self.0.new_service(cfg) }
  41. }
  42. pub type BoxService<Req, Res, Err> = Box<
  43. dyn Service<Req, Response = Res, Error = Err, Future = BoxFuture<'static, Result<Res, Err>>>
  44. + Sync
  45. + Send,
  46. >;
  47. // #[allow(dead_code)]
  48. // pub fn service<S, Req>(service: S) -> BoxService<Req, S::Response, S::Error>
  49. // where
  50. // S: Service<Req> + 'static,
  51. // Req: 'static,
  52. // S::Future: 'static,
  53. // {
  54. // Box::new(ServiceWrapper::new(service))
  55. // }
  56. impl<S, Req> Service<Req> for Box<S>
  57. where
  58. S: Service<Req> + ?Sized,
  59. {
  60. type Response = S::Response;
  61. type Error = S::Error;
  62. type Future = S::Future;
  63. fn call(&self, request: Req) -> S::Future { (**self).call(request) }
  64. }
  65. struct ServiceWrapper<S> {
  66. inner: S,
  67. }
  68. impl<S> ServiceWrapper<S> {
  69. fn new(inner: S) -> Self { Self { inner } }
  70. }
  71. impl<S, Req, Res, Err> Service<Req> for ServiceWrapper<S>
  72. where
  73. S: Service<Req, Response = Res, Error = Err>,
  74. S::Future: 'static + Send + Sync,
  75. {
  76. type Response = Res;
  77. type Error = Err;
  78. type Future = BoxFuture<'static, Result<Res, Err>>;
  79. fn call(&self, req: Req) -> Self::Future { Box::pin(self.inner.call(req)) }
  80. }
  81. struct FactoryWrapper<SF>(SF);
  82. impl<SF, Req, Cfg, Res, Err> ServiceFactory<Req> for FactoryWrapper<SF>
  83. where
  84. Req: 'static,
  85. Res: 'static,
  86. Err: 'static,
  87. SF: ServiceFactory<Req, Context = Cfg, Response = Res, Error = Err>,
  88. SF::Future: 'static,
  89. SF::Service: 'static + Send + Sync,
  90. <<SF as ServiceFactory<Req>>::Service as Service<Req>>::Future: Send + Sync + 'static,
  91. <SF as ServiceFactory<Req>>::Future: Send + Sync,
  92. {
  93. type Response = Res;
  94. type Error = Err;
  95. type Service = BoxService<Req, Res, Err>;
  96. type Context = Cfg;
  97. type Future = BoxFuture<'static, Result<Self::Service, Self::Error>>;
  98. fn new_service(&self, cfg: Cfg) -> Self::Future {
  99. let f = self.0.new_service(cfg);
  100. Box::pin(async {
  101. f.await
  102. .map(|s| Box::new(ServiceWrapper::new(s)) as Self::Service)
  103. })
  104. }
  105. }