tcp_request/request/request_builder/impl.rs
1use crate::*;
2
3/// Default implementation for RequestBuilder.
4impl Default for RequestBuilder {
5 /// Creates a default RequestBuilder instance.
6 ///
7 /// # Returns
8 ///
9 /// - `RequestBuilder` - A new RequestBuilder with default configuration.
10 #[inline(always)]
11 fn default() -> Self {
12 Self {
13 tcp_request: TcpRequest::default(),
14 builder: TcpRequest::default(),
15 }
16 }
17}
18
19/// Implementation for RequestBuilder methods.
20impl RequestBuilder {
21 /// Creates a new RequestBuilder instance.
22 ///
23 /// # Returns
24 ///
25 /// - `RequestBuilder` - A new RequestBuilder with default configuration.
26 #[inline(always)]
27 pub fn new() -> Self {
28 Self::default()
29 }
30
31 /// Sets the target host for the request.
32 ///
33 /// # Arguments
34 ///
35 /// - `T` - The host address (any type that implements Into<String>).
36 ///
37 /// # Returns
38 ///
39 /// - `&mut Self` - The builder for method chaining.
40 pub fn host<T>(&mut self, host: T) -> &mut Self
41 where
42 T: Into<String>,
43 {
44 let _ = self.tcp_request.config.write().map(|mut data| {
45 data.host = host.into();
46 });
47 self
48 }
49
50 /// Sets the target port for the request.
51 ///
52 /// # Arguments
53 ///
54 /// - `usize` - The port number.
55 ///
56 /// # Returns
57 ///
58 /// - `&mut Self` - The builder for method chaining.
59 pub fn port(&mut self, port: usize) -> &mut Self {
60 let _ = self.tcp_request.config.write().map(|mut data| {
61 data.port = port;
62 });
63 self
64 }
65
66 /// Sets the buffer size for the request.
67 ///
68 /// # Arguments
69 ///
70 /// - `usize` - The buffer size in bytes.
71 ///
72 /// # Returns
73 ///
74 /// - `&mut Self` - The builder for method chaining.
75 pub fn buffer(&mut self, buffer_size: usize) -> &mut Self {
76 let _ = self.tcp_request.config.write().map(|mut data| {
77 data.buffer_size = buffer_size;
78 });
79 self
80 }
81
82 /// Sets the timeout for the request in milliseconds.
83 ///
84 /// # Arguments
85 ///
86 /// - `u64` - The timeout duration in milliseconds.
87 ///
88 /// # Returns
89 ///
90 /// - `&mut Self` - The builder for method chaining.
91 pub fn timeout(&mut self, timeout: u64) -> &mut Self {
92 let _ = self.tcp_request.config.write().map(|mut data| {
93 data.timeout = timeout;
94 });
95 self
96 }
97
98 /// Builds and returns the configured request.
99 ///
100 /// # Returns
101 ///
102 /// - `BoxRequestTrait` - A boxed request trait object ready for use.
103 pub fn build(&mut self) -> BoxRequestTrait {
104 self.builder = self.tcp_request.clone();
105 self.tcp_request = TcpRequest::default();
106 Box::new(self.builder.clone())
107 }
108}