Skip to main content

tcplane/config/
impl.rs

1use crate::*;
2
3/// Provides a default implementation for ServerConfigData.
4impl Default for ServerConfigData {
5    /// Creates a new ServerConfigData instance with default values.
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - A new instance with default configuration.
10    #[inline(always)]
11    fn default() -> Self {
12        Self {
13            host: DEFAULT_HOST.to_owned(),
14            port: DEFAULT_PORT,
15            buffer_size: DEFAULT_BUFFER_SIZE,
16        }
17    }
18}
19
20/// Provides a default implementation for ServerConfig.
21impl Default for ServerConfig {
22    /// Creates a new ServerConfig instance with default values.
23    ///
24    /// # Returns
25    ///
26    /// - `Self` - A new instance wrapping default ServerConfigData.
27    #[inline(always)]
28    fn default() -> Self {
29        Self(Arc::new(RwLock::new(ServerConfigData::default())))
30    }
31}
32
33impl ServerConfig {
34    /// Creates a new ServerConfig instance with default settings.
35    ///
36    /// # Returns
37    ///
38    /// - `Self` - A new ServerConfig instance.
39    #[inline(always)]
40    pub fn new() -> Self {
41        Self::default()
42    }
43
44    /// Acquires a read lock on the inner configuration data.
45    ///
46    /// # Returns
47    ///
48    /// - `ArcRwLockReadGuard<ServerConfigData>` - The read guard.
49    pub(crate) async fn read(&self) -> ArcRwLockReadGuard<'_, ServerConfigData> {
50        self.0.read().await
51    }
52
53    /// Acquires a write lock on the inner configuration data.
54    ///
55    /// # Returns
56    ///
57    /// - `ArcRwLockWriteGuard<ServerConfigData>` - The write guard.
58    pub(crate) async fn write(&self) -> ArcRwLockWriteGuard<'_, ServerConfigData> {
59        self.0.write().await
60    }
61
62    /// Gets a clone of the inner configuration data.
63    ///
64    /// # Returns
65    ///
66    /// - `ServerConfigData` - A clone of the configuration data.
67    pub(crate) async fn get_data(&self) -> ServerConfigData {
68        self.read().await.clone()
69    }
70
71    /// Gets the host address.
72    ///
73    /// # Returns
74    ///
75    /// - `String` - The host address.
76    pub async fn get_host(&self) -> String {
77        self.read().await.host.clone()
78    }
79
80    /// Gets the port number.
81    ///
82    /// # Returns
83    ///
84    /// - `u16` - The port number.
85    pub async fn get_port(&self) -> u16 {
86        self.read().await.port
87    }
88
89    /// Gets the buffer size.
90    ///
91    /// # Returns
92    ///
93    /// - `usize` - The buffer size in bytes.
94    pub async fn get_buffer_size(&self) -> usize {
95        self.read().await.buffer_size
96    }
97
98    /// Sets the host address.
99    ///
100    /// # Arguments
101    ///
102    /// - `Into<String>` - Type that can be converted into String.
103    ///
104    /// # Returns
105    ///
106    /// - `&Self` - Reference to self for method chaining.
107    pub async fn host<H>(&self, host: H) -> &Self
108    where
109        H: Into<String>,
110    {
111        self.write().await.host = host.into();
112        self
113    }
114
115    /// Sets the port number.
116    ///
117    /// # Arguments
118    ///
119    /// - `u16` - The port number.
120    ///
121    /// # Returns
122    ///
123    /// - `&Self` - Reference to self for method chaining.
124    pub async fn port(&self, port: u16) -> &Self {
125        self.write().await.port = port;
126        self
127    }
128
129    /// Sets the buffer size.
130    ///
131    /// # Arguments
132    ///
133    /// - `usize` - The buffer size in bytes.
134    ///
135    /// # Returns
136    ///
137    /// - `&Self` - Reference to self for method chaining.
138    pub async fn buffer_size(&self, buffer_size: usize) -> &Self {
139        self.write().await.buffer_size = buffer_size;
140        self
141    }
142}