externally_driven_async_rt/net/
unix_stream.rs1use rustix::net::{Shutdown, SocketAddrUnix};
2
3use crate::{
4 context::Context,
5 io::{AsyncRead, AsyncWrite},
6};
7
8use super::{BorrowedReadHalf, BorrowedWriteHalf, NetError, socket::Socket};
9
10#[must_use]
12pub struct UnixStream {
13 socket: Socket,
14}
15
16impl UnixStream {
17 pub(super) const fn new(socket: Socket) -> Self {
18 Self { socket }
19 }
20
21 pub async fn connect<
31 E: 'static,
32 const EVENTS: usize,
33 const TASKS: usize,
34 const TASK_SIZE: usize,
35 const ROOT_TASK_SIZE: usize,
36 const TIMERS: usize,
37 const FDS: usize,
38 >(
39 ctx: &Context<'_, E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
40 addr: &SocketAddrUnix,
41 ) -> Result<Self, NetError> {
42 let socket = Socket::connect_stream(&ctx.storage.io_waiters, ctx.selector, addr).await?;
43 Ok(Self { socket })
44 }
45
46 pub fn shutdown(&self, how: Shutdown) -> rustix::io::Result<()> {
54 self.socket.shutdown(how)
55 }
56
57 pub fn local_addr(&self) -> rustix::io::Result<SocketAddrUnix> {
63 self.socket.local_addr()
64 }
65
66 pub fn peer_addr(&self) -> rustix::io::Result<SocketAddrUnix> {
72 self.socket
73 .peer_addr()?
74 .map_or_else(|| SocketAddrUnix::new(""), TryInto::try_into)
75 }
76
77 pub const fn split(&mut self) -> (BorrowedReadHalf<'_>, BorrowedWriteHalf<'_>) {
79 (
80 BorrowedReadHalf::new(&self.socket),
81 BorrowedWriteHalf::new(&self.socket),
82 )
83 }
84}
85
86impl AsyncRead for UnixStream {
87 type Error = NetError;
88
89 fn read<
90 'a,
91 E: 'static,
92 const EVENTS: usize,
93 const TASKS: usize,
94 const TASK_SIZE: usize,
95 const ROOT_TASK_SIZE: usize,
96 const TIMERS: usize,
97 const FDS: usize,
98 >(
99 &'a mut self,
100 ctx: &'a Context<'_, E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
101 buf: &'a mut [u8],
102 ) -> impl Future<Output = Result<usize, NetError>> {
103 self.socket.recv(&ctx.storage.io_waiters, ctx.selector, buf)
104 }
105}
106
107impl AsyncWrite for UnixStream {
108 type Error = NetError;
109
110 fn write<
111 'a,
112 E: 'static,
113 const EVENTS: usize,
114 const TASKS: usize,
115 const TASK_SIZE: usize,
116 const ROOT_TASK_SIZE: usize,
117 const TIMERS: usize,
118 const FDS: usize,
119 >(
120 &'a mut self,
121 ctx: &'a Context<'_, E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
122 buf: &'a [u8],
123 ) -> impl Future<Output = Result<usize, NetError>> {
124 self.socket.send(&ctx.storage.io_waiters, ctx.selector, buf)
125 }
126
127 fn flush<
128 'a,
129 E: 'static,
130 const EVENTS: usize,
131 const TASKS: usize,
132 const TASK_SIZE: usize,
133 const ROOT_TASK_SIZE: usize,
134 const TIMERS: usize,
135 const FDS: usize,
136 >(
137 &'a mut self,
138 _ctx: &'a Context<'_, E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
139 ) -> impl Future<Output = Result<(), NetError>> {
140 core::future::ready(Ok(()))
141 }
142}