Skip to main content

externally_driven_async_rt/net/
unix_stream.rs

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