boxed.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. use crate::service::{AFPluginServiceFactory, Service};
  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: AFPluginServiceFactory<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 AFPluginServiceFactory<Req>>::Service: Sync + Send,
  12. <<SF as AFPluginServiceFactory<Req>>::Service as Service<Req>>::Future: Send + Sync,
  13. <SF as AFPluginServiceFactory<Req>>::Future: Send + Sync,
  14. {
  15. BoxServiceFactory(Box::new(FactoryWrapper(factory)))
  16. }
  17. type Inner<Cfg, Req, Res, Err> = Box<
  18. dyn AFPluginServiceFactory<
  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> AFPluginServiceFactory<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 {
  41. self.0.new_service(cfg)
  42. }
  43. }
  44. pub type BoxService<Req, Res, Err> = Box<
  45. dyn Service<Req, Response = Res, Error = Err, Future = BoxFuture<'static, Result<Res, Err>>>
  46. + Sync
  47. + Send,
  48. >;
  49. // #[allow(dead_code)]
  50. // pub fn service<S, Req>(service: S) -> BoxService<Req, S::Response, S::Error>
  51. // where
  52. // S: Service<Req> + 'static,
  53. // Req: 'static,
  54. // S::Future: 'static,
  55. // {
  56. // Box::new(ServiceWrapper::new(service))
  57. // }
  58. impl<S, Req> Service<Req> for Box<S>
  59. where
  60. S: Service<Req> + ?Sized,
  61. {
  62. type Response = S::Response;
  63. type Error = S::Error;
  64. type Future = S::Future;
  65. fn call(&self, request: Req) -> S::Future {
  66. (**self).call(request)
  67. }
  68. }
  69. struct ServiceWrapper<S> {
  70. inner: S,
  71. }
  72. impl<S> ServiceWrapper<S> {
  73. fn new(inner: S) -> Self {
  74. Self { inner }
  75. }
  76. }
  77. impl<S, Req, Res, Err> Service<Req> for ServiceWrapper<S>
  78. where
  79. S: Service<Req, Response = Res, Error = Err>,
  80. S::Future: 'static + Send + Sync,
  81. {
  82. type Response = Res;
  83. type Error = Err;
  84. type Future = BoxFuture<'static, Result<Res, Err>>;
  85. fn call(&self, req: Req) -> Self::Future {
  86. Box::pin(self.inner.call(req))
  87. }
  88. }
  89. struct FactoryWrapper<SF>(SF);
  90. impl<SF, Req, Cfg, Res, Err> AFPluginServiceFactory<Req> for FactoryWrapper<SF>
  91. where
  92. Req: 'static,
  93. Res: 'static,
  94. Err: 'static,
  95. SF: AFPluginServiceFactory<Req, Context = Cfg, Response = Res, Error = Err>,
  96. SF::Future: 'static,
  97. SF::Service: 'static + Send + Sync,
  98. <<SF as AFPluginServiceFactory<Req>>::Service as Service<Req>>::Future: Send + Sync + 'static,
  99. <SF as AFPluginServiceFactory<Req>>::Future: Send + Sync,
  100. {
  101. type Response = Res;
  102. type Error = Err;
  103. type Service = BoxService<Req, Res, Err>;
  104. type Context = Cfg;
  105. type Future = BoxFuture<'static, Result<Self::Service, Self::Error>>;
  106. fn new_service(&self, cfg: Cfg) -> Self::Future {
  107. let f = self.0.new_service(cfg);
  108. Box::pin(async {
  109. f.await
  110. .map(|s| Box::new(ServiceWrapper::new(s)) as Self::Service)
  111. })
  112. }
  113. }