externally_driven_async_rt/net/
unix_stream.rs1use rustix::net::{Shutdown, SocketAddrUnix};
2
3use crate::{
4 context::Context,
5 net::{BorrowedReadHalf, BorrowedWriteHalf, socket::Socket},
6 state::State,
7};
8
9#[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 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 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 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 pub fn shutdown(&self, how: Shutdown) -> rustix::io::Result<()> {
90 self.socket.shutdown(how)
91 }
92
93 pub fn local_addr(&self) -> rustix::io::Result<SocketAddrUnix> {
99 self.socket.local_addr()
100 }
101
102 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 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}