boxed.rs 3.5 KB

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