monoio/net/tcp/
split.rs

1use std::{io, net::SocketAddr};
2
3use super::TcpStream;
4use crate::io::{
5    as_fd::{AsReadFd, AsWriteFd, SharedFdWrapper},
6    OwnedReadHalf, OwnedWriteHalf,
7};
8
9/// OwnedReadHalf.
10pub type TcpOwnedReadHalf = OwnedReadHalf<TcpStream>;
11/// OwnedWriteHalf
12pub type TcpOwnedWriteHalf = OwnedWriteHalf<TcpStream>;
13
14impl TcpOwnedReadHalf {
15    /// Returns the remote address that this stream is connected to.
16    #[inline]
17    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
18        unsafe { &*self.0.get() }.peer_addr()
19    }
20
21    /// Returns the local address that this stream is bound to.
22    #[inline]
23    pub fn local_addr(&self) -> io::Result<SocketAddr> {
24        unsafe { &*self.0.get() }.local_addr()
25    }
26}
27
28impl AsReadFd for TcpOwnedReadHalf {
29    #[inline]
30    fn as_reader_fd(&mut self) -> &SharedFdWrapper {
31        let raw_stream = unsafe { &mut *self.0.get() };
32        raw_stream.as_reader_fd()
33    }
34}
35
36impl TcpOwnedWriteHalf {
37    /// Returns the remote address that this stream is connected to.
38    #[inline]
39    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
40        unsafe { &*self.0.get() }.peer_addr()
41    }
42
43    /// Returns the local address that this stream is bound to.
44    #[inline]
45    pub fn local_addr(&self) -> io::Result<SocketAddr> {
46        unsafe { &*self.0.get() }.local_addr()
47    }
48}
49
50impl AsWriteFd for TcpOwnedWriteHalf {
51    #[inline]
52    fn as_writer_fd(&mut self) -> &SharedFdWrapper {
53        let raw_stream = unsafe { &mut *self.0.get() };
54        raw_stream.as_writer_fd()
55    }
56}