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]
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]
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 /// - `ResponseResult` - Result indicating success or failure.
65 pub async fn send<D>(&self, data: D) -> ResponseResult
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 body.
78 ///
79 /// # Arguments
80 ///
81 /// - `AsRef<[u8]>` - The response body data (must implement AsRef<[u8]>).
82 ///
83 /// # Returns
84 ///
85 /// - `ResponseResult` - Result indicating success or failure.
86 pub async fn send_body<D>(&self, data: D) -> ResponseResult
87 where
88 D: AsRef<[u8]>,
89 {
90 self.write()
91 .await
92 .write_all(data.as_ref())
93 .await
94 .map_err(|err| ResponseError::Response(err.to_string()))?;
95 Ok(())
96 }
97
98 /// Sends multiple HTTP response bodies sequentially.
99 ///
100 /// # Arguments
101 ///
102 /// - `I: IntoIterator<Item = D>, D: AsRef<[u8]>` - The response body data list to send.
103 ///
104 /// # Returns
105 ///
106 /// - `ResponseResult` - Result indicating success or failure.
107 pub async fn send_body_list<I, D>(&self, data_iter: I) -> ResponseResult
108 where
109 I: IntoIterator<Item = D>,
110 D: AsRef<[u8]>,
111 {
112 let mut stream: RwLockWriteGuardTcpStream = self.write().await;
113 for data in data_iter {
114 stream
115 .write_all(data.as_ref())
116 .await
117 .map_err(|err| ResponseError::Response(err.to_string()))?;
118 }
119 Ok(())
120 }
121
122 /// Flushes all buffered data to the stream.
123 ///
124 /// # Returns
125 ///
126 /// - `&Self` - Reference to self for method chaining.
127 pub async fn flush(&self) -> &Self {
128 self.write().await.flush().await.ok();
129 self
130 }
131}