udp/socket/impl.rs
1use crate::*;
2
3/// Implementation of ArcRwLockUdpSocket methods.
4///
5/// Provides construction and access methods for thread-safe UDP socket.
6impl ArcRwLockUdpSocket {
7 /// Creates a new instance from existing ArcRwLock<UdpSocket>.
8 ///
9 /// # Arguments
10 ///
11 /// - `ArcRwLock<UdpSocket>` - Existing socket with read-write lock.
12 ///
13 /// # Returns
14 ///
15 /// - `ArcRwLockUdpSocket` - New wrapper instance.
16 pub fn from(arc_rw_lock_socket: ArcRwLock<UdpSocket>) -> Self {
17 Self(arc_rw_lock_socket)
18 }
19
20 /// Creates a new instance from raw UdpSocket.
21 ///
22 /// # Arguments
23 ///
24 /// - `UdpSocket` - Raw UDP socket.
25 ///
26 /// # Returns
27 ///
28 /// - `ArcRwLockUdpSocket` - New wrapper instance.
29 pub fn from_socket(socket: UdpSocket) -> Self {
30 Self(Arc::new(RwLock::new(socket)))
31 }
32
33 /// Acquires a read lock on the socket.
34 ///
35 /// # Returns
36 ///
37 /// - `RwLockReadGuardUdpSocket` - Read guard for the socket.
38 pub async fn get_read_lock(&'_ self) -> RwLockReadGuardUdpSocket<'_> {
39 self.0.read().await
40 }
41
42 /// Acquires a write lock on the socket.
43 ///
44 /// # Returns
45 ///
46 /// - `RwLockWriteGuardUdpSocket` - Write guard for the socket.
47 pub async fn get_write_lock(&'_ self) -> RwLockWriteGuardUdpSocket<'_> {
48 self.0.write().await
49 }
50}