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.
10pub struct UnixStream {
11    socket: Socket,
12}
13
14impl UnixStream {
15    pub(crate) fn new(socket: Socket) -> Self {
16        Self { socket }
17    }
18
19    /// Connects to a given address.
20    pub async fn connect<
21        S: State,
22        const TASKS: usize,
23        const TASK_SIZE: usize,
24        const ROOT_TASK_SIZE: usize,
25        const TIMERS: usize,
26        const FDS: usize,
27    >(
28        ctx: &Context<'_, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
29        addr: &SocketAddrUnix,
30    ) -> rustix::io::Result<Self> {
31        let socket = Socket::connect_stream(ctx, addr).await?;
32        Ok(Self { socket })
33    }
34
35    /// Reads from the underlying socket into a given buffer.
36    pub fn read<
37        'a,
38        'ctx,
39        S: State,
40        const TASKS: usize,
41        const TASK_SIZE: usize,
42        const ROOT_TASK_SIZE: usize,
43        const TIMERS: usize,
44        const FDS: usize,
45    >(
46        &'a mut self,
47        ctx: &'a Context<'ctx, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
48        buf: &'a mut [u8],
49    ) -> impl Future<Output = rustix::io::Result<usize>> {
50        self.socket.read(ctx, buf)
51    }
52
53    /// Writes from a given buffer into the underlying socket.
54    pub fn write<
55        'a,
56        'ctx,
57        S: State,
58        const TASKS: usize,
59        const TASK_SIZE: usize,
60        const ROOT_TASK_SIZE: usize,
61        const TIMERS: usize,
62        const FDS: usize,
63    >(
64        &'a mut self,
65        ctx: &'a Context<'ctx, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
66        buf: &'a [u8],
67    ) -> impl Future<Output = rustix::io::Result<usize>> {
68        self.socket.write(ctx, buf)
69    }
70
71    /// Shuts down the connection (which also impacts potentially existing read/write halves).
72    ///
73    /// The underlying file descriptor stays open until this value is dropped.
74    pub fn shutdown(&self, how: Shutdown) -> rustix::io::Result<()> {
75        self.socket.shutdown(how)
76    }
77
78    /// Returns local address.
79    pub fn local_addr(&self) -> rustix::io::Result<SocketAddrUnix> {
80        self.socket.local_addr()
81    }
82
83    /// Returns peer address.
84    pub fn peer_addr(&self) -> rustix::io::Result<SocketAddrUnix> {
85        self.socket.peer_addr()
86    }
87
88    /// Splits the [`UnixStream`] into two halves: reader and writer.
89    pub fn split(&mut self) -> (BorrowedReadHalf<'_>, BorrowedWriteHalf<'_>) {
90        (
91            BorrowedReadHalf::new(&self.socket),
92            BorrowedWriteHalf::new(&self.socket),
93        )
94    }
95}
96
97impl core::fmt::Debug for UnixStream {
98    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
99        f.write_str("UnixStream")
100    }
101}