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