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