jay_config/
io.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//! Tools for IO operations.

use {
    crate::_private::PollableId,
    futures_util::{io::AsyncRead, AsyncWrite},
    std::{
        future::poll_fn,
        io::{self, ErrorKind, IoSlice, IoSliceMut, Read, Write},
        os::fd::{AsFd, AsRawFd},
        pin::Pin,
        task::{ready, Context, Poll},
    },
    thiserror::Error,
    uapi::c,
};

#[derive(Debug, Error)]
enum AsyncError {
    #[error("Could not retrieve the file description flags")]
    GetFl(#[source] io::Error),
    #[error("Could not set the file description flags")]
    SetFl(#[source] io::Error),
    #[error("This configuration has already been destroyed")]
    Destroyed,
    #[error("The compositor could not create the necessary data structures: {0}")]
    CompositorSetup(String),
    #[error("Could not poll the file description: {0}")]
    Poll(String),
}

impl From<AsyncError> for io::Error {
    fn from(value: AsyncError) -> Self {
        io::Error::new(ErrorKind::Other, value)
    }
}

/// An async adapter for types implementing [`AsFd`].
pub struct Async<T> {
    id: PollableIdWrapper,
    t: Option<T>,
}

impl<T> Unpin for Async<T> {}

struct PollableIdWrapper {
    id: PollableId,
}

impl Drop for PollableIdWrapper {
    fn drop(&mut self) {
        get!().remove_pollable(self.id);
    }
}

impl<T> Async<T>
where
    T: AsFd,
{
    /// Creates a new async adapter.
    ///
    /// This takes ownership of the file description and duplicates the file descriptor.
    /// You should not modify the file description while this object is in use, otherwise
    /// the behavior is undefined.
    pub fn new(t: T) -> Result<Self, io::Error> {
        Ok(Self::new_(t)?)
    }

    fn new_(t: T) -> Result<Self, AsyncError> {
        let fd = t.as_fd();
        let fl = uapi::fcntl_getfl(fd.as_raw_fd())
            .map_err(|e| AsyncError::GetFl(io::Error::from_raw_os_error(e.0)))?;
        uapi::fcntl_setfl(fd.as_raw_fd(), fl | c::O_NONBLOCK)
            .map_err(|e| AsyncError::SetFl(io::Error::from_raw_os_error(e.0)))?;
        let id = get!(Err(AsyncError::Destroyed))
            .create_pollable(fd.as_raw_fd())
            .map_err(AsyncError::CompositorSetup)?;
        Ok(Self {
            id: PollableIdWrapper { id },
            t: Some(t),
        })
    }
}

impl<T> Async<T> {
    /// Unwraps the underlying object.
    ///
    /// Note that the underlying object is still non-blocking at this point.
    pub fn unwrap(self) -> T {
        self.t.unwrap()
    }

    fn poll_(&self, writable: bool, cx: &mut Context<'_>) -> Poll<Result<(), AsyncError>> {
        get!(Poll::Ready(Err(AsyncError::Destroyed)))
            .poll_io(self.id.id, writable, cx)
            .map_err(AsyncError::Poll)
    }

    async fn poll(&self, writable: bool) -> Result<(), io::Error> {
        poll_fn(|cx| self.poll_(writable, cx)).await?;
        Ok(())
    }

    /// Waits for the file description to become readable.
    pub async fn readable(&self) -> Result<(), io::Error> {
        self.poll(false).await
    }

    /// Waits for the file description to become writable.
    pub async fn writable(&self) -> Result<(), io::Error> {
        self.poll(true).await
    }
}

impl<T> AsRef<T> for Async<T> {
    fn as_ref(&self) -> &T {
        self.t.as_ref().unwrap()
    }
}

impl<T> AsMut<T> for Async<T> {
    fn as_mut(&mut self) -> &mut T {
        self.t.as_mut().unwrap()
    }
}

fn poll_io<T, R>(
    slf: &mut Async<T>,
    writable: bool,
    cx: &mut Context<'_>,
    mut f: impl FnMut(&mut Async<T>) -> io::Result<R>,
) -> Poll<io::Result<R>> {
    loop {
        ready!(slf.poll_(writable, cx))?;
        match f(slf) {
            Err(e) if e.kind() == ErrorKind::WouldBlock => {}
            res => return Poll::Ready(res),
        }
    }
}

impl<T> AsyncRead for Async<T>
where
    T: Read,
{
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<io::Result<usize>> {
        poll_io(self.get_mut(), false, cx, |slf| slf.as_mut().read(buf))
    }

    fn poll_read_vectored(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        bufs: &mut [IoSliceMut<'_>],
    ) -> Poll<io::Result<usize>> {
        poll_io(self.get_mut(), false, cx, |slf| {
            slf.as_mut().read_vectored(bufs)
        })
    }
}

impl<T> AsyncWrite for Async<T>
where
    T: Write,
{
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<io::Result<usize>> {
        poll_io(self.get_mut(), true, cx, |slf| slf.as_mut().write(buf))
    }

    fn poll_write_vectored(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        bufs: &[IoSlice<'_>],
    ) -> Poll<io::Result<usize>> {
        poll_io(self.get_mut(), true, cx, |slf| {
            slf.as_mut().write_vectored(bufs)
        })
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        poll_io(self.get_mut(), true, cx, |slf| slf.as_mut().flush())
    }

    fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        self.get_mut().t.take();
        Poll::Ready(Ok(()))
    }
}