http_type/stream/
impl.rs

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