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
9pub struct UnixStream {
11 socket: Socket,
12}
13
14impl UnixStream {
15 pub(crate) fn new(socket: Socket) -> Self {
16 Self { socket }
17 }
18
19 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 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 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 pub fn shutdown(&self, how: Shutdown) -> rustix::io::Result<()> {
75 self.socket.shutdown(how)
76 }
77
78 pub fn local_addr(&self) -> rustix::io::Result<SocketAddrUnix> {
80 self.socket.local_addr()
81 }
82
83 pub fn peer_addr(&self) -> rustix::io::Result<SocketAddrUnix> {
85 self.socket.peer_addr()
86 }
87
88 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}