Skip to main content

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    /// - `RwLockReadGuard<'_, TcpStream>` - 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    /// - `RwLockWriteGuard<'_, TcpStream>` - 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        Ok(self.write().await.write_all(data.as_ref()).await?)
90    }
91
92    /// Sends HTTP response data over the stream.
93    ///
94    /// # Arguments
95    ///
96    /// - `AsRef<[u8]>` - The response data to send (must implement AsRef<[u8]>).
97    ///
98    /// # Panics
99    ///
100    /// Panics if the write operation fails.
101    pub async fn send<D>(&self, data: D)
102    where
103        D: AsRef<[u8]>,
104    {
105        self.try_send(data).await.unwrap();
106    }
107
108    /// Sends HTTP response body.
109    ///
110    /// # Arguments
111    ///
112    /// - `AsRef<[u8]>` - The response body data (must implement AsRef<[u8]>).
113    ///
114    /// # Returns
115    ///
116    /// - `Result<(), ResponseError>` - Result indicating success or failure.
117    pub async fn try_send_body<D>(&self, data: D) -> Result<(), ResponseError>
118    where
119        D: AsRef<[u8]>,
120    {
121        Ok(self.write().await.write_all(data.as_ref()).await?)
122    }
123
124    /// Sends HTTP response body.
125    ///
126    /// # Arguments
127    ///
128    /// - `AsRef<[u8]>` - The response body data (must implement AsRef<[u8]>).
129    ///
130    /// # Panics
131    ///
132    /// Panics if the write operation fails.
133    pub async fn send_body<D>(&self, data: D)
134    where
135        D: AsRef<[u8]>,
136    {
137        self.try_send_body(data).await.unwrap();
138    }
139
140    /// Sends multiple HTTP response bodies sequentially.
141    ///
142    /// # Arguments
143    ///
144    /// - `I: IntoIterator<Item = D>, D: AsRef<[u8]>` - The response body data list to send.
145    ///
146    /// # Returns
147    ///
148    /// - `Result<(), ResponseError>` - Result indicating success or failure.
149    pub async fn try_send_body_list<I, D>(&self, data_iter: I) -> Result<(), ResponseError>
150    where
151        I: IntoIterator<Item = D>,
152        D: AsRef<[u8]>,
153    {
154        let mut stream: RwLockWriteGuard<'_, TcpStream> = self.write().await;
155        for data in data_iter {
156            stream.write_all(data.as_ref()).await?;
157        }
158        Ok(())
159    }
160
161    /// Sends multiple HTTP response bodies sequentially.
162    ///
163    /// # Arguments
164    ///
165    /// - `I: IntoIterator<Item = D>, D: AsRef<[u8]>` - The response body data list to send.
166    ///
167    /// # Panics
168    ///
169    /// Panics if any write operation fails.
170    pub async fn send_body_list<I, D>(&self, data_iter: I)
171    where
172        I: IntoIterator<Item = D>,
173        D: AsRef<[u8]>,
174    {
175        self.try_send_body_list(data_iter).await.unwrap();
176    }
177
178    /// Flushes all buffered data to the stream.
179    ///
180    /// # Returns
181    ///
182    /// - `Result<(), ResponseError>` - Result indicating success or failure.
183    pub async fn try_flush(&self) -> Result<(), ResponseError> {
184        Ok(self.write().await.flush().await?)
185    }
186
187    /// Flushes all buffered data to the stream.
188    ///
189    /// # Panics
190    ///
191    /// Panics if the flush operation fails.
192    pub async fn flush(&self) {
193        self.try_flush().await.unwrap();
194    }
195}