Skip to main content

externally_driven_async_rt/net/
unix_stream.rs

1use rustix::net::{Shutdown, SocketAddrUnix};
2
3use crate::{
4    context::Context,
5    io::{AsyncRead, AsyncWrite},
6    net::{BorrowedReadHalf, BorrowedWriteHalf, socket::Socket},
7    state::State,
8};
9
10/// A Unix stream.
11#[must_use]
12pub struct UnixStream {
13    socket: Socket,
14}
15
16impl UnixStream {
17    pub(crate) const fn new(socket: Socket) -> Self {
18        Self { socket }
19    }
20
21    /// Connects to a given address.
22    ///
23    /// # Errors
24    ///
25    /// Returns an error if a syscall fails.
26    ///
27    /// # Cancel safety
28    ///
29    /// This method is cancel safe.
30    pub async fn connect<
31        S: State,
32        const TASKS: usize,
33        const TASK_SIZE: usize,
34        const ROOT_TASK_SIZE: usize,
35        const TIMERS: usize,
36        const FDS: usize,
37    >(
38        ctx: &Context<'_, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
39        addr: &SocketAddrUnix,
40    ) -> rustix::io::Result<Self> {
41        let socket = Socket::connect_stream(ctx, addr).await?;
42        Ok(Self { socket })
43    }
44
45    /// Shuts down the connection (which also impacts potentially existing read/write halves).
46    ///
47    /// The underlying file descriptor stays open until this value is dropped.
48    ///
49    /// # Errors
50    ///
51    /// Returns an error if a syscall fails.
52    pub fn shutdown(&self, how: Shutdown) -> rustix::io::Result<()> {
53        self.socket.shutdown(how)
54    }
55
56    /// Returns local address.
57    ///
58    /// # Errors
59    ///
60    /// Returns an error if a syscall fails.
61    pub fn local_addr(&self) -> rustix::io::Result<SocketAddrUnix> {
62        self.socket.local_addr()
63    }
64
65    /// Returns peer address.
66    ///
67    /// # Errors
68    ///
69    /// Returns an error if a syscall fails.
70    pub fn peer_addr(&self) -> rustix::io::Result<SocketAddrUnix> {
71        self.socket
72            .peer_addr()?
73            .map_or_else(|| SocketAddrUnix::new(""), TryInto::try_into)
74    }
75
76    /// Splits the [`UnixStream`] into two halves: reader and writer.
77    pub const fn split(&mut self) -> (BorrowedReadHalf<'_>, BorrowedWriteHalf<'_>) {
78        (
79            BorrowedReadHalf::new(&self.socket),
80            BorrowedWriteHalf::new(&self.socket),
81        )
82    }
83}
84
85impl AsyncRead for UnixStream {
86    type Error = rustix::io::Errno;
87
88    fn read<
89        'a,
90        S: State,
91        const TASKS: usize,
92        const TASK_SIZE: usize,
93        const ROOT_TASK_SIZE: usize,
94        const TIMERS: usize,
95        const FDS: usize,
96    >(
97        &'a mut self,
98        ctx: &'a Context<'_, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
99        buf: &'a mut [u8],
100    ) -> impl Future<Output = rustix::io::Result<usize>> {
101        self.socket.read(ctx, buf)
102    }
103}
104
105impl AsyncWrite for UnixStream {
106    type Error = rustix::io::Errno;
107
108    fn write<
109        'a,
110        S: State,
111        const TASKS: usize,
112        const TASK_SIZE: usize,
113        const ROOT_TASK_SIZE: usize,
114        const TIMERS: usize,
115        const FDS: usize,
116    >(
117        &'a mut self,
118        ctx: &'a Context<'_, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
119        buf: &'a [u8],
120    ) -> impl Future<Output = rustix::io::Result<usize>> {
121        self.socket.write(ctx, buf)
122    }
123
124    fn flush<
125        'a,
126        S: State,
127        const TASKS: usize,
128        const TASK_SIZE: usize,
129        const ROOT_TASK_SIZE: usize,
130        const TIMERS: usize,
131        const FDS: usize,
132    >(
133        &'a mut self,
134        _ctx: &'a Context<'_, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
135    ) -> impl Future<Output = rustix::io::Result<()>> {
136        core::future::ready(Ok(()))
137    }
138}
139
140impl core::fmt::Debug for UnixStream {
141    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
142        f.write_str("UnixStream")
143    }
144}