http_type/stream/
impl.rs

1use crate::*;
2
3impl From<ArcRwLock<TcpStream>> for ArcRwLockStream {
4    /// Converts an Arc<RwLock<TcpStream>> into an ArcRwLockStream.
5    ///
6    /// # Arguments
7    ///
8    /// - `Arc<RwLock<TcpStream>>` - The stream to wrap.
9    ///
10    /// # Returns
11    ///
12    /// - `ArcRwLockStream` - The new stream wrapper.
13    #[inline(always)]
14    fn from(arc_rw_lock_stream: ArcRwLock<TcpStream>) -> Self {
15        Self(arc_rw_lock_stream)
16    }
17}
18
19impl ArcRwLockStream {
20    /// Creates a new ArcRwLockStream from a TcpStream.
21    ///
22    /// Wraps the stream in an Arc<RwLock<_>>.
23    ///
24    /// # Arguments
25    ///
26    /// - `TcpStream` - The raw stream to wrap.
27    ///
28    /// # Returns
29    ///
30    /// - `ArcRwLockStream` - The new thread-safe stream wrapper.
31    #[inline(always)]
32    pub fn from_stream(stream: TcpStream) -> Self {
33        Self(arc_rwlock(stream))
34    }
35
36    /// Gets a read lock on the inner TcpStream.
37    ///
38    /// Allows shared read access to the stream.
39    ///
40    /// # Returns
41    ///
42    /// - `RwLockReadGuardTcpStream` - The read guard for the stream.
43    pub async fn read(&'_ self) -> RwLockReadGuardTcpStream<'_> {
44        self.get_0().read().await
45    }
46
47    /// Gets a write lock on the inner TcpStream.
48    ///
49    /// Allows exclusive write access to the stream.
50    ///
51    /// # Returns
52    ///
53    /// - `RwLockWriteGuardTcpStream` - The write guard for the stream.
54    pub(crate) async fn write(&'_ self) -> RwLockWriteGuardTcpStream<'_> {
55        self.get_0().write().await
56    }
57
58    /// Sends HTTP response data over the stream.
59    ///
60    /// # Arguments
61    ///
62    /// - `AsRef<[u8]>` - The response data to send (must implement AsRef<[u8]>).
63    ///
64    /// # Returns
65    ///
66    /// - `Result<(), ResponseError>` - Result indicating success or failure.
67    pub async fn try_send<D>(&self, data: D) -> Result<(), ResponseError>
68    where
69        D: AsRef<[u8]>,
70    {
71        self.write()
72            .await
73            .write_all(data.as_ref())
74            .await
75            .map_err(|error| ResponseError::Response(error.to_string()))?;
76        Ok(())
77    }
78
79    /// Sends HTTP response data over the stream.
80    ///
81    /// # Arguments
82    ///
83    /// - `AsRef<[u8]>` - The response data to send (must implement AsRef<[u8]>).
84    ///
85    /// # Panics
86    ///
87    /// Panics if the write operation fails.
88    pub async fn send<D>(&self, data: D)
89    where
90        D: AsRef<[u8]>,
91    {
92        self.try_send(data).await.unwrap();
93    }
94
95    /// Sends HTTP response body.
96    ///
97    /// # Arguments
98    ///
99    /// - `AsRef<[u8]>` - The response body data (must implement AsRef<[u8]>).
100    ///
101    /// # Returns
102    ///
103    /// - `Result<(), ResponseError>` - Result indicating success or failure.
104    pub async fn try_send_body<D>(&self, data: D) -> Result<(), ResponseError>
105    where
106        D: AsRef<[u8]>,
107    {
108        self.write()
109            .await
110            .write_all(data.as_ref())
111            .await
112            .map_err(|error| ResponseError::Response(error.to_string()))?;
113        Ok(())
114    }
115
116    /// Sends HTTP response body.
117    ///
118    /// # Arguments
119    ///
120    /// - `AsRef<[u8]>` - The response body data (must implement AsRef<[u8]>).
121    ///
122    /// # Panics
123    ///
124    /// Panics if the write operation fails.
125    pub async fn send_body<D>(&self, data: D)
126    where
127        D: AsRef<[u8]>,
128    {
129        self.try_send_body(data).await.unwrap();
130    }
131
132    /// Sends multiple HTTP response bodies sequentially.
133    ///
134    /// # Arguments
135    ///
136    /// - `I: IntoIterator<Item = D>, D: AsRef<[u8]>` - The response body data list to send.
137    ///
138    /// # Returns
139    ///
140    /// - `Result<(), ResponseError>` - Result indicating success or failure.
141    pub async fn try_send_body_list<I, D>(&self, data_iter: I) -> Result<(), ResponseError>
142    where
143        I: IntoIterator<Item = D>,
144        D: AsRef<[u8]>,
145    {
146        let mut stream: RwLockWriteGuardTcpStream = self.write().await;
147        for data in data_iter {
148            stream
149                .write_all(data.as_ref())
150                .await
151                .map_err(|error| ResponseError::Response(error.to_string()))?;
152        }
153        Ok(())
154    }
155
156    /// Sends multiple HTTP response bodies sequentially.
157    ///
158    /// # Arguments
159    ///
160    /// - `I: IntoIterator<Item = D>, D: AsRef<[u8]>` - The response body data list to send.
161    ///
162    /// # Panics
163    ///
164    /// Panics if any write operation fails.
165    pub async fn send_body_list<I, D>(&self, data_iter: I)
166    where
167        I: IntoIterator<Item = D>,
168        D: AsRef<[u8]>,
169    {
170        self.try_send_body_list(data_iter).await.unwrap();
171    }
172
173    /// Flushes all buffered data to the stream.
174    ///
175    /// # Returns
176    ///
177    /// - `Result<(), ResponseError>` - Result indicating success or failure.
178    pub async fn try_flush(&self) -> Result<(), ResponseError> {
179        self.write()
180            .await
181            .flush()
182            .await
183            .map_err(|error| ResponseError::FlushError(error.to_string()))
184    }
185
186    /// Flushes all buffered data to the stream.
187    ///
188    /// # Panics
189    ///
190    /// Panics if the flush operation fails.
191    pub async fn flush(&self) {
192        self.try_flush().await.unwrap();
193    }
194}