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 pub fn from(arc_rw_lock_stream: ArcRwLock<TcpStream>) -> Self {
14 Self(arc_rw_lock_stream)
15 }
16
17 /// Creates a new ArcRwLockStream from a TcpStream.
18 ///
19 /// Wraps the stream in an Arc<RwLock<_>>.
20 ///
21 /// # Arguments
22 ///
23 /// - `TcpStream` - The raw stream to wrap.
24 ///
25 /// # Returns
26 ///
27 /// - `ArcRwLockStream` - The new thread-safe stream wrapper.
28 pub fn from_stream(stream: TcpStream) -> Self {
29 Self(arc_rwlock(stream))
30 }
31
32 /// Gets a read lock on the inner TcpStream.
33 ///
34 /// Allows shared read access to the stream.
35 ///
36 /// # Returns
37 ///
38 /// - `RwLockReadGuardTcpStream` - The read guard for the stream.
39 pub async fn read(&'_ self) -> RwLockReadGuardTcpStream<'_> {
40 self.get_0().read().await
41 }
42
43 /// Gets a write lock on the inner TcpStream.
44 ///
45 /// Allows exclusive write access to the stream.
46 ///
47 /// # Returns
48 ///
49 /// - `RwLockWriteGuardTcpStream` - The write guard for the stream.
50 pub(crate) async fn write(&'_ self) -> RwLockWriteGuardTcpStream<'_> {
51 self.get_0().write().await
52 }
53
54 /// Sends HTTP response data over the stream.
55 ///
56 /// # Arguments
57 ///
58 /// - `AsRef<[u8]>` - The response data to send (must implement AsRef<[u8]>).
59 ///
60 /// # Returns
61 ///
62 /// - `ResponseResult` - Result indicating success or failure.
63 pub async fn send<D>(&self, data: D) -> ResponseResult
64 where
65 D: AsRef<[u8]>,
66 {
67 self.write()
68 .await
69 .write_all(data.as_ref())
70 .await
71 .map_err(|err| ResponseError::Response(err.to_string()))?;
72 Ok(())
73 }
74
75 /// Sends HTTP response body.
76 ///
77 /// # Arguments
78 ///
79 /// - `AsRef<[u8]>` - The response body data (must implement AsRef<[u8]>).
80 ///
81 /// # Returns
82 ///
83 /// - `ResponseResult` - Result indicating success or failure.
84 pub async fn send_body<D>(&self, data: D) -> ResponseResult
85 where
86 D: AsRef<[u8]>,
87 {
88 self.write()
89 .await
90 .write_all(data.as_ref())
91 .await
92 .map_err(|err| ResponseError::Response(err.to_string()))?;
93 Ok(())
94 }
95
96 /// Sends multiple HTTP response bodies sequentially.
97 ///
98 /// # Arguments
99 ///
100 /// - `I: IntoIterator<Item = D>, D: AsRef<[u8]>` - The response body data list to send.
101 ///
102 /// # Returns
103 ///
104 /// - `ResponseResult` - Result indicating success or failure.
105 pub async fn send_body_list<I, D>(&self, data_iter: I) -> ResponseResult
106 where
107 I: IntoIterator<Item = D>,
108 D: AsRef<[u8]>,
109 {
110 let mut stream: RwLockWriteGuardTcpStream = self.write().await;
111 for data in data_iter {
112 stream
113 .write_all(data.as_ref())
114 .await
115 .map_err(|err| ResponseError::Response(err.to_string()))?;
116 }
117 Ok(())
118 }
119
120 /// Flushes all buffered data to the stream.
121 ///
122 /// # Returns
123 ///
124 /// - `&Self` - Reference to self for method chaining.
125 pub async fn flush(&self) -> &Self {
126 let _ = self.write().await.flush();
127 self
128 }
129}