dope-fiber 0.8.0

The manifold runtime
Documentation
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use std::io;
use std::marker::PhantomData;
use std::mem;
use std::net::Shutdown;
use std::pin::Pin;
use std::task::Poll;

use dope::ProvidedView;
use o3::buffer::{Bytes, Retained, Shared};

use crate::net::port::{Port, RecvChunkResult, RecvInto, SendIdle};
use crate::{Context, Fiber, Waker};
use dope::driver::token::Token;

pub trait Host<'d> {
    fn port(&self) -> &Port<'d>;

    fn recv_into(&self, id: Token, dst: &mut [u8]) -> RecvInto {
        self.port().recv_into(id, dst)
    }

    fn recv_chunk(&self, id: Token) -> RecvChunkResult<'d> {
        self.port().recv_chunk(id)
    }

    fn recv_waker(&self, id: Token, waker: Waker<'d>) {
        self.port().recv_waker(id, waker);
    }

    fn clear_recv_waker(&self, id: Token) {
        self.port().clear_recv_waker(id);
    }

    fn send_waker(&self, id: Token, waker: Waker<'d>) {
        self.port().send_waker(id, waker);
    }

    fn clear_send_waker(&self, id: Token) {
        self.port().clear_send_waker(id);
    }

    fn send(&self, id: Token, bytes: Shared) {
        self.port().send(id, bytes);
    }

    fn send_idle(&self, id: Token) -> SendIdle {
        self.port().send_idle(id)
    }

    fn shutdown(&self, id: Token, how: i32) {
        self.port().shutdown(id, how);
    }

    fn close(&self, id: Token) {
        self.port().close(id);
    }
}

pub enum RecvBuffer<'d> {
    Owned(Bytes<Retained>),
    Provided(ProvidedView<'d>),
}

impl RecvBuffer<'_> {
    pub fn as_slice(&self) -> &[u8] {
        match self {
            Self::Owned(value) => value.as_slice(),
            Self::Provided(value) => value.as_slice(),
        }
    }

    pub fn len(&self) -> usize {
        self.as_slice().len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn advance(&mut self, count: usize) {
        match self {
            Self::Owned(value) => value.advance(count),
            Self::Provided(value) => value.advance(count),
        }
    }
}

impl AsRef<[u8]> for RecvBuffer<'_> {
    fn as_ref(&self) -> &[u8] {
        self.as_slice()
    }
}

pub struct Io<'d, H: Host<'d>> {
    host: H,
    id: Token,
    brand: PhantomData<fn(&'d ()) -> &'d ()>,
}

#[derive(Clone, Copy)]
enum Interest {
    Recv,
    Send,
}

struct Registration<'a, 'd, H: Host<'d>> {
    io: &'a mut Io<'d, H>,
    interest: Interest,
    armed: bool,
}

impl<'a, 'd, H: Host<'d>> Registration<'a, 'd, H> {
    fn new(io: &'a mut Io<'d, H>, interest: Interest) -> Self {
        Self {
            io,
            interest,
            armed: false,
        }
    }

    fn arm(&mut self, waker: Waker<'d>) {
        match self.interest {
            Interest::Recv => self.io.set_recv_waker(waker),
            Interest::Send => self.io.set_send_waker(waker),
        }
        self.armed = true;
    }

    fn clear(&mut self) {
        if !self.armed {
            return;
        }
        match self.interest {
            Interest::Recv => self.io.clear_recv_waker(),
            Interest::Send => self.io.clear_send_waker(),
        }
        self.armed = false;
    }

    fn complete(&mut self) {
        // Every transition that can make a registered operation ready takes
        // its waiter before waking the task. The port therefore no longer
        // owns this registration once a re-poll observes readiness. Keep the
        // explicit clear in Drop for the cancellation path, where no wake has
        // transferred ownership back to the task.
        self.armed = false;
    }

    fn poll_recv(
        &mut self,
        cx: Pin<&mut Context<'_, 'd>>,
        dst: &mut [u8],
        done: &mut bool,
    ) -> Poll<io::Result<usize>> {
        let result = if dst.is_empty() {
            Poll::Ready(Ok(0))
        } else {
            match self.io.try_recv_into(dst) {
                RecvInto::Bytes(count) => Poll::Ready(Ok(count)),
                RecvInto::Failed(error) => Poll::Ready(Err(error)),
                RecvInto::Pending => {
                    self.arm(unsafe { cx.waker_unchecked() });
                    Poll::Pending
                }
            }
        };
        if result.is_ready() {
            self.complete();
            *done = true;
        }
        result
    }

    fn poll_send(
        &mut self,
        cx: Pin<&mut Context<'_, 'd>>,
        done: &mut bool,
    ) -> Poll<io::Result<()>> {
        let result = match self.io.try_send_idle() {
            SendIdle::Idle => Poll::Ready(Ok(())),
            SendIdle::Failed(error) => Poll::Ready(Err(error)),
            SendIdle::Pending => {
                self.arm(unsafe { cx.waker_unchecked() });
                Poll::Pending
            }
        };
        if result.is_ready() {
            self.complete();
            *done = true;
        }
        result
    }
}

impl<'d, H: Host<'d>> Drop for Registration<'_, 'd, H> {
    fn drop(&mut self) {
        self.clear();
    }
}

impl<'d, H: Host<'d>> Io<'d, H> {
    pub(super) fn new(host: H, id: Token) -> Self {
        Self {
            host,
            id,
            brand: PhantomData,
        }
    }

    fn host(&self) -> &H {
        &self.host
    }

    fn try_recv_into(&mut self, dst: &mut [u8]) -> RecvInto {
        self.host().recv_into(self.id, dst)
    }

    fn try_recv_chunk(&mut self) -> RecvChunkResult<'d> {
        self.host().recv_chunk(self.id)
    }

    fn set_recv_waker(&mut self, waker: Waker<'d>) {
        self.host().recv_waker(self.id, waker);
    }

    fn set_send_waker(&mut self, waker: Waker<'d>) {
        self.host().send_waker(self.id, waker);
    }

    fn clear_recv_waker(&mut self) {
        self.host().clear_recv_waker(self.id);
    }

    fn clear_send_waker(&mut self) {
        self.host().clear_send_waker(self.id);
    }

    fn submit_send(&mut self, bytes: Shared) {
        self.host().send(self.id, bytes);
    }

    fn try_send_idle(&mut self) -> SendIdle {
        self.host().send_idle(self.id)
    }

    pub fn shutdown(&mut self, how: Shutdown) -> io::Result<()> {
        let how = match how {
            Shutdown::Read => libc::SHUT_RD,
            Shutdown::Write => libc::SHUT_WR,
            Shutdown::Both => libc::SHUT_RDWR,
        };
        self.host().shutdown(self.id, how);
        Ok(())
    }

    pub fn read_into<'a>(
        &'a mut self,
        buf: &'a mut [u8],
    ) -> impl Fiber<'d, Output = io::Result<usize>> + 'a {
        ReadInto {
            registration: Registration::new(self, Interest::Recv),
            buf,
            done: false,
        }
    }

    pub fn read_chunk(
        &mut self,
    ) -> impl Fiber<'d, Output = io::Result<Option<RecvBuffer<'d>>>> + '_ {
        ReadChunk {
            registration: Registration::new(self, Interest::Recv),
            done: false,
        }
    }

    pub fn write_all<'a>(
        &'a mut self,
        data: &'a [u8],
    ) -> impl Fiber<'d, Output = io::Result<()>> + 'a {
        WriteAll {
            registration: Registration::new(self, Interest::Send),
            data,
            submitted: false,
            done: false,
        }
    }

    pub fn write_all_shared<'a>(
        &'a mut self,
        bytes: Shared,
    ) -> impl Fiber<'d, Output = io::Result<()>> + 'a {
        WriteAllShared {
            registration: Registration::new(self, Interest::Send),
            bytes: Some(bytes),
            done: false,
        }
    }

    pub fn write_all_static<'a>(
        &'a mut self,
        bytes: &'static [u8],
    ) -> impl Fiber<'d, Output = io::Result<()>> + 'a {
        self.write_all_shared(Shared::from_static(bytes))
    }

    pub fn read<'a>(
        &'a mut self,
        buf: Vec<u8>,
    ) -> impl Fiber<'d, Output = (io::Result<usize>, Vec<u8>)> + 'a {
        Read {
            registration: Registration::new(self, Interest::Recv),
            buf,
            done: false,
        }
    }
}

struct ReadInto<'a, 'd, H: Host<'d>> {
    registration: Registration<'a, 'd, H>,
    buf: &'a mut [u8],
    done: bool,
}

impl<'d, H: Host<'d>> Fiber<'d> for ReadInto<'_, 'd, H> {
    type Output = io::Result<usize>;

    fn poll(self: Pin<&mut Self>, cx: Pin<&mut Context<'_, 'd>>) -> Poll<Self::Output> {
        let this = self.get_mut();
        assert!(!this.done, "fiber::Io::read_into polled after completion");
        this.registration.poll_recv(cx, this.buf, &mut this.done)
    }
}

struct ReadChunk<'a, 'd, H: Host<'d>> {
    registration: Registration<'a, 'd, H>,
    done: bool,
}

impl<'d, H: Host<'d>> Fiber<'d> for ReadChunk<'_, 'd, H> {
    type Output = io::Result<Option<RecvBuffer<'d>>>;

    fn poll(self: Pin<&mut Self>, cx: Pin<&mut Context<'_, 'd>>) -> Poll<Self::Output> {
        let this = self.get_mut();
        assert!(!this.done, "fiber::Io::read_chunk polled after completion");
        let result = match this.registration.io.try_recv_chunk() {
            RecvChunkResult::Chunk(chunk) => Poll::Ready(Ok(Some(chunk))),
            RecvChunkResult::Closed => Poll::Ready(Ok(None)),
            RecvChunkResult::Failed(error) => Poll::Ready(Err(error)),
            RecvChunkResult::Pending => {
                this.registration.arm(unsafe { cx.waker_unchecked() });
                Poll::Pending
            }
        };
        if result.is_ready() {
            this.registration.complete();
            this.done = true;
        }
        result
    }
}

struct WriteAll<'a, 'd, H: Host<'d>> {
    registration: Registration<'a, 'd, H>,
    data: &'a [u8],
    submitted: bool,
    done: bool,
}

impl<'d, H: Host<'d>> Fiber<'d> for WriteAll<'_, 'd, H> {
    type Output = io::Result<()>;

    fn poll(self: Pin<&mut Self>, cx: Pin<&mut Context<'_, 'd>>) -> Poll<Self::Output> {
        let this = self.get_mut();
        assert!(!this.done, "fiber::Io::write_all polled after completion");
        if !this.submitted {
            if this.data.is_empty() {
                this.done = true;
                return Poll::Ready(Ok(()));
            }
            this.registration
                .io
                .submit_send(Shared::copy_from_slice(this.data));
            this.submitted = true;
        }
        this.registration.poll_send(cx, &mut this.done)
    }
}

struct WriteAllShared<'a, 'd, H: Host<'d>> {
    registration: Registration<'a, 'd, H>,
    bytes: Option<Shared>,
    done: bool,
}

impl<'d, H: Host<'d>> Fiber<'d> for WriteAllShared<'_, 'd, H> {
    type Output = io::Result<()>;

    fn poll(self: Pin<&mut Self>, cx: Pin<&mut Context<'_, 'd>>) -> Poll<Self::Output> {
        let this = self.get_mut();
        assert!(
            !this.done,
            "fiber::Io::write_all_shared polled after completion"
        );
        if let Some(bytes) = this.bytes.take() {
            if bytes.is_empty() {
                this.done = true;
                return Poll::Ready(Ok(()));
            }
            this.registration.io.submit_send(bytes);
        }
        this.registration.poll_send(cx, &mut this.done)
    }
}

struct Read<'a, 'd, H: Host<'d>> {
    registration: Registration<'a, 'd, H>,
    buf: Vec<u8>,
    done: bool,
}

impl<'d, H: Host<'d>> Fiber<'d> for Read<'_, 'd, H> {
    type Output = (io::Result<usize>, Vec<u8>);

    fn poll(self: Pin<&mut Self>, cx: Pin<&mut Context<'_, 'd>>) -> Poll<Self::Output> {
        let this = self.get_mut();
        assert!(!this.done, "fiber::Io::read polled after completion");
        let result = this
            .registration
            .poll_recv(cx, &mut this.buf, &mut this.done);
        match result {
            Poll::Ready(result) => Poll::Ready((result, mem::take(&mut this.buf))),
            Poll::Pending => Poll::Pending,
        }
    }
}

impl<'d, H: Host<'d>> Drop for Io<'d, H> {
    fn drop(&mut self) {
        self.host().close(self.id);
    }
}