nyquest_interface/async/
any.rs1use std::any::Any;
11use std::fmt;
12use std::pin::Pin;
13use std::sync::Arc;
14
15use futures_core::future::BoxFuture;
16
17use super::backend::AsyncResponse;
18use super::Request;
19use crate::client::ClientOptions;
20use crate::Result;
21
22pub trait AnyAsyncBackend: Send + Sync + 'static {
26 fn create_async_client(
28 &self,
29 options: ClientOptions,
30 ) -> BoxFuture<'_, Result<Arc<dyn AnyAsyncClient>>>;
31}
32
33pub trait AnyAsyncClient: Any + Send + Sync + 'static {
37 fn describe(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
39 fn request(&self, req: Request) -> BoxFuture<'_, Result<Pin<Box<dyn AnyAsyncResponse>>>>;
41}
42
43pub trait AnyAsyncResponse: super::MaybeAsyncRead + Any + Send + Sync + 'static {
47 fn describe(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
49 fn status(&self) -> u16;
51 fn content_length(&self) -> Option<u64>;
53 fn get_header(&self, header: &str) -> Result<Vec<String>>;
55 fn text(self: Pin<&mut Self>) -> BoxFuture<'_, Result<String>>;
57 fn bytes(self: Pin<&mut Self>) -> BoxFuture<'_, Result<Vec<u8>>>;
59}
60
61impl<R> AnyAsyncResponse for R
65where
66 R: AsyncResponse,
67{
68 fn status(&self) -> u16 {
69 AsyncResponse::status(self)
70 }
71
72 fn content_length(&self) -> Option<u64> {
73 AsyncResponse::content_length(self)
74 }
75
76 fn get_header(&self, header: &str) -> Result<Vec<String>> {
77 AsyncResponse::get_header(self, header)
78 }
79
80 fn text(self: Pin<&mut Self>) -> BoxFuture<'_, Result<String>> {
81 Box::pin(AsyncResponse::text(self))
82 }
83
84 fn bytes(self: Pin<&mut Self>) -> BoxFuture<'_, Result<Vec<u8>>> {
85 Box::pin(AsyncResponse::bytes(self))
86 }
87
88 fn describe(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 AsyncResponse::describe(self, f)
90 }
91}
92
93impl<A> AnyAsyncBackend for A
94where
95 A: super::backend::AsyncBackend,
96 A::AsyncClient: super::backend::AsyncClient,
97{
98 fn create_async_client(
99 &self,
100 options: ClientOptions,
101 ) -> BoxFuture<'_, Result<Arc<dyn AnyAsyncClient>>> {
102 Box::pin(async {
103 super::backend::AsyncBackend::create_async_client(self, options)
104 .await
105 .map(|client| Arc::new(client) as Arc<dyn AnyAsyncClient>)
106 }) as _
107 }
108}
109
110impl<A> AnyAsyncClient for A
111where
112 A: super::backend::AsyncClient,
113{
114 fn describe(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115 super::backend::AsyncClient::describe(self, f)
116 }
117
118 fn request(&self, req: Request) -> BoxFuture<'_, Result<Pin<Box<dyn AnyAsyncResponse>>>> {
119 Box::pin(async {
120 self.request(req)
121 .await
122 .map(|res| Box::pin(res) as Pin<Box<dyn AnyAsyncResponse>>)
123 }) as _
124 }
125}